String Format for Double [C#]

2024-06-23 02:58

本文主要是介绍String Format for Double [C#],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

小数点后的数字

这个例子格式的两位小数固定长度的字符串。对于小数点后两位使用模式“0.00”。如果浮点数小数少,右边其余数字将是零。如果有更多的小数,将四舍五入。

[C#]
<span class="comments" style="color: rgb(0, 170, 0);">// just two decimal places</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.00}"</span>, 123.4567);      <span class="comments" style="color: rgb(0, 170, 0);">// "123.46"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.00}"</span>, 123.4);         <span class="comments" style="color: rgb(0, 170, 0);">// "123.40"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.00}"</span>, 123.0);         <span class="comments" style="color: rgb(0, 170, 0);">// "123.00"</span>

在下面的例子格式字符串浮点数与小数。例如最大的小数点后两位使用模式 “0.##“.

[C#]
<span class="comments" style="color: rgb(0, 170, 0);">// max. two decimal places</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.##}"</span>, 123.4567);      <span class="comments" style="color: rgb(0, 170, 0);">// "123.46"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.##}"</span>, 123.4);         <span class="comments" style="color: rgb(0, 170, 0);">// "123.4"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.##}"</span>, 123.0);         <span class="comments" style="color: rgb(0, 170, 0);">// "123"</span>

小数点前的数字

如果你想要一个浮点数之前有任何数量最少的数字,小数点使用N次小数点前的零。例如对于小数点后两位使用模式“00.0”

[C#]
<span class="comments" style="color: rgb(0, 170, 0);">// at least two digits before decimal point</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:00.0}"</span>, 123.4567);      <span class="comments" style="color: rgb(0, 170, 0);">// "123.5"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:00.0}"</span>, 23.4567);       <span class="comments" style="color: rgb(0, 170, 0);">// "23.5"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:00.0}"</span>, 3.4567);        <span class="comments" style="color: rgb(0, 170, 0);">// "03.5"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:00.0}"</span>, -3.4567);       <span class="comments" style="color: rgb(0, 170, 0);">// "-03.5"</span>

千位分隔符

要格式化双字符串使用千位分隔符使用零和逗号分隔前一个平常的浮点格式模式,例如:模式“0,0.0”格式的数量,使用千位分隔符,并有一个小数位。

[C#]
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0,0.0}"</span>, 12345.67);     <span class="comments" style="color: rgb(0, 170, 0);">// "12,345.7"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0,0}"</span>, 12345.67);       <span class="comments" style="color: rgb(0, 170, 0);">// "12,346" 四舍五入</span>

0

0和1之间的浮点数可以在两种方式进行格式化,带或不带前导零小数点前。要格式化#前点没有使用前导零的数字。例如“#.0”格式有一个小数位和零到N小数点前的数字(例如“5”或“123.5”)。

下面的代码演示如何格式化一个零(double类型)。

[C#]
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.0}"</span>, 0.0);            <span class="comments" style="color: rgb(0, 170, 0);">// "0.0"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.#}"</span>, 0.0);            <span class="comments" style="color: rgb(0, 170, 0);">// "0"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:#.0}"</span>, 0.0);            <span class="comments" style="color: rgb(0, 170, 0);">// ".0"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:#.#}"</span>, 0.0);            <span class="comments" style="color: rgb(0, 170, 0);">// ""</span>

用空格对齐数字

右对齐:在”,“后不变。其次是数量的空格,例如类型逗号“0,10:0.0”(可以使用String.Format方法,在double.ToString方法不是)。左对齐:在”,“后,用"-"

[C#]
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0,10:0.0}"</span>, 123.4567);    <span class="comments" style="color: rgb(0, 170, 0);">// "     123.5"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0,-10:0.0}"</span>, 123.4567);   <span class="comments" style="color: rgb(0, 170, 0);">// "123.5     "</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0,10:0.0}"</span>, -123.4567);   <span class="comments" style="color: rgb(0, 170, 0);">// "    -123.5"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0,-10:0.0}"</span>, -123.4567);  <span class="comments" style="color: rgb(0, 170, 0);">// "-123.5    "</span>

自定义格式为负数和零

如果需要使用自定义格式为负浮点数或零,用分号分隔“;”分模式三个部分。第一部分正数格式,负数第二部分格式和第三部分格式零。如果你省略了最后一节,零使用的第一部分将被格式化。

[C#]
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.00;minus 0.00;zero}"</span>, 123.4567);   <span class="comments" style="color: rgb(0, 170, 0);">// "123.46"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.00;minus 0.00;zero}"</span>, -123.4567);  <span class="comments" style="color: rgb(0, 170, 0);">// "minus 123.46"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0.00;minus 0.00;zero}"</span>, 0.0);        <span class="comments" style="color: rgb(0, 170, 0);">// "zero"</span>

一些有趣的例子

正如你可能会注意到,在前面的例子,你可以放入任何文本格式的模式,例如:之前,通常的模式“my number is
0.0”。你甚至可以把零之间的任何文本,例如: “0aaa.bbb0”。

[C#]
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:my number is 0.0}"</span>, 12.3);   <span class="comments" style="color: rgb(0, 170, 0);">// "my number is 12.3"</span>
<span class="type" style="color: rgb(43, 145, 175);">String</span>.Format(<span class="string" style="color: rgb(255, 0, 0);">"{0:0aaa.bbb0}"</span>, 12.3);          <span class="comments" style="color: rgb(0, 170, 0);">// "12aaa.bbb3"</span>

See also

  • [C#] String Format for Int – format (align) integer numbers
  • [C#] String Format for DateTime – format date and time
  • [C#] IFormatProvider for Numbers – format and parse float numbers using CultureInfo
  • [C#] Custom IFormatProvider – string formatting with custom IFormatProvider
  • [C#] Align String with Spaces – how to align text to the right or left
  • [C#] Indent String with Spaces – how to indent text with repeated spaces
  • MSDN Examples – MSDN examples for custom numeric formatting
  • String.Format – MSDN – method to format strings
  • Double.ToString – MSDN – formats double to string
源:http://www.csharp-examples.net/string-format-double/

这篇关于String Format for Double [C#]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086096

相关文章

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时

C# $字符串插值的使用

《C#$字符串插值的使用》本文介绍了C#中的字符串插值功能,详细介绍了使用$符号的实现方式,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录$ 字符使用方式创建内插字符串包含不同的数据类型控制内插表达式的格式控制内插表达式的对齐方式内插表达式中使用转义序列内插表达式中使用

C#中的Converter的具体应用

《C#中的Converter的具体应用》C#中的Converter提供了一种灵活的类型转换机制,本文详细介绍了Converter的基本概念、使用场景,具有一定的参考价值,感兴趣的可以了解一下... 目录Converter的基本概念1. Converter委托2. 使用场景布尔型转换示例示例1:简单的字符串到

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

C#读写文本文件的多种方式详解

《C#读写文本文件的多种方式详解》这篇文章主要为大家详细介绍了C#中各种常用的文件读写方式,包括文本文件,二进制文件、CSV文件、JSON文件等,有需要的小伙伴可以参考一下... 目录一、文本文件读写1. 使用 File 类的静态方法2. 使用 StreamReader 和 StreamWriter二、二进

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin