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#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

C#文件复制异常:"未能找到文件"的解决方案与预防措施

《C#文件复制异常:未能找到文件的解决方案与预防措施》在C#开发中,文件操作是基础中的基础,但有时最基础的File.Copy()方法也会抛出令人困惑的异常,当targetFilePath设置为D:2... 目录一个看似简单的文件操作问题问题重现与错误分析错误代码示例错误信息根本原因分析全面解决方案1. 确保

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject