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#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

C#使用yield关键字实现提升迭代性能与效率

《C#使用yield关键字实现提升迭代性能与效率》yield关键字在C#中简化了数据迭代的方式,实现了按需生成数据,自动维护迭代状态,本文主要来聊聊如何使用yield关键字实现提升迭代性能与效率,感兴... 目录前言传统迭代和yield迭代方式对比yield延迟加载按需获取数据yield break显式示迭

c# checked和unchecked关键字的使用

《c#checked和unchecked关键字的使用》C#中的checked关键字用于启用整数运算的溢出检查,可以捕获并抛出System.OverflowException异常,而unchecked... 目录在 C# 中,checked 关键字用于启用整数运算的溢出检查。默认情况下,C# 的整数运算不会自

IDEA如何将String类型转json格式

《IDEA如何将String类型转json格式》在Java中,字符串字面量中的转义字符会被自动转换,但通过网络获取的字符串可能不会自动转换,为了解决IDEA无法识别JSON字符串的问题,可以在本地对字... 目录问题描述问题原因解决方案总结问题描述最近做项目需要使用Ai生成json,可生成String类型

C#实现获得某个枚举的所有名称

《C#实现获得某个枚举的所有名称》这篇文章主要为大家详细介绍了C#如何实现获得某个枚举的所有名称,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... C#中获得某个枚举的所有名称using System;using System.Collections.Generic;usi

C# 读写ini文件操作实现

《C#读写ini文件操作实现》本文主要介绍了C#读写ini文件操作实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录一、INI文件结构二、读取INI文件中的数据在C#应用程序中,常将INI文件作为配置文件,用于存储应用程序的