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# 中变量未赋值能用吗,各种类型的初始值是什么

对于一个局部变量,如果未赋值,是不能使用的 对于属性,未赋值,也能使用有系统默认值,默认值如下: 对于 int 类型,默认值是 0;对于 int? 类型,默认值是 null;对于 bool 类型,默认值是 false;对于 bool? 类型,默认值是 null;对于 string 类型,默认值是 null;对于 string? 类型,哈哈,没有这种写法,会出错;对于 DateTime 类型,默

C#中,decimal类型使用

在Microsoft SQL Server中numeric类型,在C#中使用的时候,需要用decimal类型与其对应,不能使用int等类型。 SQL:numeric C#:decimal

算法与数据结构面试宝典——回溯算法详解(C#,C++)

文章目录 1. 回溯算法的定义及应用场景2. 回溯算法的基本思想3. 递推关系式与回溯算法的建立4. 状态转移方法5. 边界条件与结束条件6. 算法的具体实现过程7. 回溯算法在C#,C++中的实际应用案例C#示例C++示例 8. 总结回溯算法的主要特点与应用价值 回溯算法是一种通过尝试各种可能的组合来找到所有解的算法。这种算法通常用于解决组合问题,如排列、组合、棋盘游

C# 命名管道中客户端访问服务器时,出现“对路径的访问被拒绝”

先还原一下我出现错误的情景:我用C#控制台写了一个命名管道服务器,然后用ASP.NET写了一个客户端访问服务器,运行之后出现了下面的错误: 原因:服务器端的访问权限不够,所以是服务器端的问题,需要增加访问权限。(网上很多都说是文件夹的权限不够,情况不同,不适用于我这种情况) 解决办法: (1)在服务器端相应地方添加以下代码。 PipeSecurity pse = new PipeSec

1_CString char* string之间的关系

CString转char*,string string转char*,CString char* 转CString,string 一、CString转char*,string //字串转换测试 CString CString1; std::string string1; CHAR* char1=NULL; //1string1=CString1.GetBuffer();CStri

如何通过示例将旧版 C# 转换为 C# 12

随着 C# 的不断发展,每个新版本都会引入强大的新功能,从而提高语言的功能和可读性。通过从旧版本的 C# 迁移到 C# 12,您可以获得更高效、更易于维护和更具表现力的代码。 由于代码库遗留、公司限制以及对旧语言功能的熟悉,许多开发人员仍在使用旧版本的 C#。升级似乎很困难,但现代版本的 C# 具有显著的优势,例如更好的性能、增强的功能和更高的安全性。 通过增量重构、试点项目和团队培训逐步

C# 日志框架Serilog使用

1、框架和说明        C#日志框架Serilog支持多种场景输出,简单验证了一下,比较方便        包的安装,推荐直接使用“推荐NuGet包管理器”安装Serilog.AspNetCore,常见的组件都已经集成在一个包中,使用比较方便 2、配置文件        Serilog可以由配置文件来定义行为,而且配置文件的修改即时生效。参考配置文件如下: {"Serilog":

query string parameters 和request payload

HTTP请求中,如果是get请求,那么表单参数以name=value&name1=value1的形式附到url的后; post请求:表单参数是在请求体中,也是name=value&name1=value1的形式在请求。 export const voucherDetailAdd=(token,formStr) =>{return axios.post(`${base}/voucher/deta

【Java】ArrayListString转化为String数组问题

Java的容器类Collections中toArray()方法,可以把诸如ArrayList<String>的动态数组、不定长转化静态数组、定长数组String[] 但是,如下的转化方式是错误的。 [java]  view plain copy String[] strArray = (String[]) arrayList.toArray();   如果这样执行会导致

C#启动另外一个C#程序,并传递参数

第一个程序:             using System.ComponentModel; using System.IO;         private void button1_Click(object sender, EventArgs e)         {             string target = Path.GetDirectory