Why does the 'p' string format for percent multiply the value by 100 before formatting it with the percentage sign?
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#PFormatString
The Percent ("P") Format Specifier
The percent ("P") format specifier multiplies a number by 100 and converts it to a string that represents a percentage. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision supplied by the current PercentDecimalDigits property is used.
Is there any way to prevent the value from being multiplied by 100 before formatting? Rather than doing:
(value / 100).ToString("p")
As to the why, "percent" literally means "out of one hundred", so 50% is mathematically equivalent to 0.50
. As to formatting, why not just add a percent sign?
value + "%"
... or something like this:
value.ToString("#.00\\%")
You normally work with decimal percents inside of code, like 0.5
and 1.0
, but the user likes to see nice whole numbers with a percent sign tacked on the end.
Percent means "out of 100
" and clearly your decimal percents are out of 1
. Therefore, .ToString("p")
multiplies your number by 100
and then appends a percent sign.
It's just the definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With