C# 为什么 0.ToString("#.##") 返回空字符串而不是 0.00 或至少 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9001603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does 0.ToString("#.##") return an empty string instead of 0.00 or at least 0?
提问by user960567
Why does 0.ToString("#.##")return an empty string? Shouldn't it be 0.00or at least 0?
为什么0.ToString("#.##")返回一个空字符串?不应该是0.00或至少是 0吗?
采纳答案by Rich O'Kelly
#in the string format indicate that the value is optional. If you wish to get the output 0.00you need the following:
#在字符串格式中表示该值是可选的。如果您希望获得输出,0.00您需要以下内容:
0.ToString("0.00");
See here for the custom numeric formatsthat can be passed to this method.
回答by Andrew Barber
Because in a format string, the #is used to signify an optional character placeholder; it's only used if neededto represent the number.
因为在格式字符串中,#用于表示可选字符占位符;它仅在需要表示数字时使用。
If you do this instead: 0.ToString("0.##");you get: 0
如果你这样做:0.ToString("0.##");你得到:0
Interestingly, if you do this: 0.ToString("#.0#");you get: .0
有趣的是,如果你这样做:0.ToString("#.0#");你会得到:.0
If you want all three digits: 0.ToString("0.00");produces: 0.00
如果你想要所有三个数字:0.ToString("0.00");产生:0.00
From the comments to this answer, your argument seems to be,
从评论到这个答案,你的论点似乎是,
it should show '0', because why would anyone ever want to see an empty string if the numeric value is 0?
它应该显示“0”,因为如果数值为 0,为什么有人会想要看到一个空字符串?
The response is simple: You have the choicehow you wish it to be displayed. That's what the custom format strings are for. You have simply chosen the incorrect format string for your needs.
答案很简单:您可以选择希望的显示方式。这就是自定义格式字符串的用途。您只是根据需要选择了不正确的格式字符串。
回答by Miguel Madero
According to the documentation about the Digit Placeholder.
根据有关数字占位符的文档。
If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string. This specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed. The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "##" would result in the value 35.
如果正在格式化的值在格式字符串中出现“#”的位置有一个数字,则该数字将被复制到结果字符串中。否则,结果字符串中的该位置不会存储任何内容。如果“0”不是有效数字,即使“0”是字符串中唯一的数字,此说明符也不会显示它。如果它是所显示数字中的有效数字,它将显示“0”字符。“##”格式字符串使值四舍五入到小数点前最接近的数字,其中始终使用远离零的四舍五入。例如,使用“##”格式化 34.5 将导致值 35。
If you want the zero to display use the Zero PlaceHolder
如果要显示零,请使用零占位符
f the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the result string. The position of the leftmost '0' before the decimal point and the rightmost '0' after the decimal point determines the range of digits that are always present in the result string.
The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.
f 被格式化的值在格式字符串中出现“0”的位置有一个数字,然后该数字被复制到结果字符串中。小数点前最左边的“0”和小数点后最右边的“0”的位置决定了结果字符串中始终存在的数字范围。
“00”说明符使值四舍五入到小数点前最接近的数字,其中始终使用远离零的四舍五入。例如,使用“00”格式化 34.5 将导致值 35。
回答by nektobit
Try this 0.ToString("#,##; #,##;0")
尝试这个 0.ToString("#,##; #,##;0")
The first section applies to positive values, the second section applies to negative values, and the third section applies to zeros.
第一部分适用于正值,第二部分适用于负值,第三部分适用于零。
回答by user11530336
Use it like this:
像这样使用它:
0.ToString("#0.##")
0after #will ensure to set output value to 0 if the value is 0 else it will display the value.
So 0.0.ToString("#0.##")=0.00and 10.ToString("#.##")=10.00
0after#将确保将输出值设置为 0 如果值为 0 否则它将显示该值。所以0.0.ToString("#0.##")=0.00和10.ToString("#.##")=10.00

