Phill
Phill

Reputation: 18796

Why does percentage format specifier multiply by 100?

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")

Upvotes: 20

Views: 13511

Answers (3)

Mihir
Mihir

Reputation: 85

If you don't want to multiply the value by 100 , just want the percentage % besides the value entered , i.e. VALUE.00 % if value entered otherwise default 0.00%, then the answer is

PERCENTAGE_FORMAT = "#0.00\%";

It definitely worked for me !!

Upvotes: 1

StriplingWarrior
StriplingWarrior

Reputation: 156469

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\\%")

Upvotes: 25

Blender
Blender

Reputation: 298136

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.

Upvotes: 5

Related Questions