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#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处