Search code examples
c#number-formatting

In c#, is there any reason for having adjacent #'s on the left side of a decimal format?


I was looking at some old code in a team codebase that was doing some decimal formatting like this:

exampleDecimal.ToString("###.##")

For this example, I don't think the first 2 #'s are useful. Since all of the #'s before the . are optional digits, wouldn't this always give the same result:

exampleDecimal.ToString("#.##")

However, I read through this c# documentation, and I wasn't able to conclusively decide whether these 2 formats above were technically identical. Will they always produce the same output, or is there some case I'm missing?


Solution

  • From your cited 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.

    So, yes, both format strings will always produce the same results.