Reputation: 3729
Can anyone tell me why does this string.Format() doesn't show the 1st value?
long countLoop = 0;
long countTotal = 3721;
string.Format("Processed {0:#,###,###} lines of {1:#,###,###} ({2:P0})", countLoop, countTotal, ((double)countLoop / countTotal));
The result I got are
Processed lines of 3,721 (0 %)
But if I replaced countTotal with a number 1
string.Format("Processed {0:#,###,###} lines of {1:#,###,###} ({2:P0})", 1, countTotal, ((double)countLoop / countTotal));
I get this
Processed 1 lines of 3,721 (0 %).
Is there something about string.Format that I don't know about?
Upvotes: 2
Views: 181
Reputation: 149050
See the documentation on the the "#" custom format specifier:
Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string.
If you'd like to display "0" in this case, take a look at the "0" custom format specifier:
If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string.
This should work for you:
string.Format(
"Processed {0:#,###,##0} lines of {1:#,###,###} ({2:P0})",
countLoop, countTotal, ((double)countLoop / countTotal));
Upvotes: 5
Reputation: 216313
Change your string format to
string.Format("Processed {0:#,###,##0} lines of {1:#,###,###} ({2:P0})", countLoop, countTotal, ((double)countLoop / countTotal));
this will print 0 when the countLoop is zero.
As other have said before me, the # placeholder doesn't print the zero when it is not a significant digit.
Note I have fixed your indexing inside the format string.
You have only three parameters, so the index goes from 0 to 2 (not 3 as you have at the end ({3:P0})
Upvotes: 4