最好的方法来显示小数点后面的零 [英] Best way to display decimal without trailing zeroes
本文介绍了最好的方法来显示小数点后面的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// decimal(十进制)是否有一个显示格式化程序将输出小数点作为这些字符串在c#中的表示形式? - >字符串
20 - > 20
20.00 - > 20
20.5 - > 20.5
20.5000 - > 20.5
20.125 - > 20.125
20.12500 - > 20.125
0.000 - > 0
{0#}将循环,并且使用一些Trim类型的函数将不能使用bound数字列在网格中。
解决方案
您是否有最大数量的小数位,你将永远需要显示? (你的例子有5个最大值)。
如果是这样的话,我会认为使用0。#####格式将会做你想要的。 / b>
static void Main(string [] args)
{
var dList = new decimal [] {20 ,20.00m,20.5m,20.5000m,20.125m,20.12500m,0.000m};
foreach(dList中的var d)
Console.WriteLine(d.ToString(0。#####));
}
Is there a display formatter that will output decimals as these string representations in c# without doing any rounding?
// decimal -> string
20 -> 20
20.00 -> 20
20.5 -> 20.5
20.5000 -> 20.5
20.125 -> 20.125
20.12500 -> 20.125
0.000 -> 0
{0.#} will round, and using some Trim type function will not work with a bound numeric column in a grid.
解决方案
Do you have a maximum number of decimal places you'll ever need to display? (Your examples have a max of 5).
If so, I would think that formatting with "0.#####" would do what you want.
static void Main(string[] args)
{
var dList = new decimal[] { 20, 20.00m, 20.5m, 20.5000m, 20.125m, 20.12500m, 0.000m };
foreach (var d in dList)
Console.WriteLine(d.ToString("0.#####"));
}
这篇关于最好的方法来显示小数点后面的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文