我一直在寻找这个,但似乎无法找到答案.我有以下小数和我想要的 String.Format
I have been searching for this but cannot seem to find the answer. I have the following decimals with the corresponding output that I want from String.Format
100.00 -> 100100.50 -> 100.50100.51 -> 100.51
100.00 -> 100 100.50 -> 100.50 100.51 -> 100.51
我的问题是,我似乎找不到一种格式,既可以在 100.50 的末尾保留 0,又可以从 100 中删除 2 个零.
My problem is that I cannot seem to find a format which will keep the 0 on the end of 100.50 as well as remove the 2 zeros from 100.
非常感谢任何帮助.
编辑为了更清楚一些.我有十进制类型的变量,它们只能保留 2 个小数位.基本上我想显示2个小数位,如果它们存在或不存在,我不想在100.50变成100.5的情况下显示一个小数位
EDIT For some more clarity. I have variables of type decimal, they are only ever going to be 2 decimal places. Basically I want to display 2 decimal places if they exist or none, I don't want to display one decimal place in the case of 100.50 becoming 100.5
推荐答案你可以使用这个:
string s = number.ToString("0.00"); if (s.EndsWith("00")) { s = number.ToString("0"); }更多推荐
格式化十进制 C#
发布评论