Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataFormatString formats percentage wrong

Tags:

c#

.net

asp.net

I have this field from the database which is:

DataType: Numeric (7, 3)

So will allow max number

9999.99

Into the database.

I put the number

10.000

Into the database, and now I'm retrieving out of the database using:

<asp:GridView>

I specify:

<asp:BoundField DataFormatString="{0:p}" />

And it outputs

1,000.00%

Which is suppose to be

10.00%

Why isn't it?

I also can't put <% %> server runtime tags into a:

<asp:TemplateField />

Why is this?

like image 929
Callum Linington Avatar asked Dec 01 '22 05:12

Callum Linington


2 Answers

Percentages are stored as decimals where 0 = 0% and 1 = 100%. You should store your value as 0.1, not 10.

The line <asp:BoundField DataFormatString="{0:p}" /> will format your value of 10.000 to 1,000.00%.

See here

like image 149
Kevin Aenmey Avatar answered Dec 05 '22 15:12

Kevin Aenmey


It's behaving exactly as described in the Standard Numeric Format Strings page on MSDN:

Result: Number multiplied by 100 and displayed with a percent symbol.

So either you shouldn't use that format string, or you should keep your value as a proportion of 1, rather than as a percentage.

like image 32
Jon Skeet Avatar answered Dec 05 '22 17:12

Jon Skeet