Csharp/C#教程:如果有,我如何格式化只包括小数分享


如果有,我如何格式化只包括小数

如果我只想显示小数而不是整数,那么格式化小数的最佳方法是什么。

例如:

decimal amount = 1000M decimal vat = 12.50M 

格式化时我想:

 Amount: 1000 (not 1000.0000) Vat: 12.5 (not 12.50) 

  decimal one = 1000M; decimal two = 12.5M; Console.WriteLine(one.ToString("0.##")); Console.WriteLine(two.ToString("0.##")); 

由user1676558更新了以下评论

试试这个:

 decimal one = 1000M; decimal two = 12.5M; decimal three = 12.567M; Console.WriteLine(one.ToString("G")); Console.WriteLine(two.ToString("G")); Console.WriteLine(three.ToString("G")); 

对于十进制值,“G”格式说明符的默认精度为29位,省略精度时始终使用定点表示法,因此这与“0。########”相同#####################”。

与“0。##”不同,它将显示所有有效小数位(小数值不能超过29位小数)。

“G29”格式说明符类似,但如果更紧凑则可以使用科学记数法(请参阅标准数字格式字符串 )。

从而:

上述就是C#学习教程:如果有,我如何格式化只包括小数分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 decimal d = 0.0000000000000000000012M; Console.WriteLine(d.ToString("G")); // Uses fixed-point notation Console.WriteLine(d.ToString("G29"); // Uses scientific notation 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/951043.html

(0)
上一篇 2021年11月18日
下一篇 2021年11月18日

精彩推荐