可能重复:
C# How to remove 0 from the end of a decimal value
Best way to display decimal without trailing zeros in c#十进制转换为字符串转换问题
我有小数变量它的价值
2.50
2.25
我怎样才能把它转换成字符串所以我可以得到
2.5
2.25
问题是2.50必须是2.5的字符串。
可能重复:
C# How to remove 0 from the end of a decimal value
Best way to display decimal without trailing zeros in c#十进制转换为字符串转换问题
我有小数变量它的价值
2.50
2.25
我怎样才能把它转换成字符串所以我可以得到
2.5
2.25
问题是2.50必须是2.5的字符串。
String.Format("{0:0.##}", 2.50);
您也可以将其转换为字符串,然后做String.Trim('0');
修剪尾随零了。
你可以做这样this example显示:
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
(基本上,使用String.Format
功能)