在 C# 中,是否有任何理由在十进制格式的左侧有相邻的#?
In c#, is there any reason for having adjacent #'s on the left side of a decimal format?
我正在查看团队代码库中的一些旧代码,这些代码正在执行一些十进制格式,如下所示:
exampleDecimal.ToString("###.##")
对于这个例子,我认为前 2 个 #
没有用。由于 .
之前的所有 #
都是可选数字,这不会总是给出相同的结果:
exampleDecimal.ToString("#.##")
但是,我通读了 this c# documentation,但我无法最终确定上述两种格式在技术上是否相同。它们会始终产生相同的输出,还是我遗漏了某些情况?
来自您引用的文档link:
The "#" Custom Specifier
The "#" custom format specifier serves as a digit-placeholder symbol. [...]
Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.
所以,是的,两种格式字符串将始终产生相同的结果。
是的,#
和 ##
产生相同的输出。
但是,当您使用多个 #
符号时,您可以嵌入其他字符。例如,#,###
的格式将生成一个在千位之间用逗号分隔的数字,如果它有足够的数字。
我正在查看团队代码库中的一些旧代码,这些代码正在执行一些十进制格式,如下所示:
exampleDecimal.ToString("###.##")
对于这个例子,我认为前 2 个 #
没有用。由于 .
之前的所有 #
都是可选数字,这不会总是给出相同的结果:
exampleDecimal.ToString("#.##")
但是,我通读了 this c# documentation,但我无法最终确定上述两种格式在技术上是否相同。它们会始终产生相同的输出,还是我遗漏了某些情况?
来自您引用的文档link:
The "#" Custom Specifier The "#" custom format specifier serves as a digit-placeholder symbol. [...]
Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.
所以,是的,两种格式字符串将始终产生相同的结果。
是的,#
和 ##
产生相同的输出。
但是,当您使用多个 #
符号时,您可以嵌入其他字符。例如,#,###
的格式将生成一个在千位之间用逗号分隔的数字,如果它有足够的数字。