How do I in .NET format a number as percentage without showing the percentage sign?
If I have the number 0.13
and use the format string {0:P0}
the output is 13 %
.
However I would like to get 13
instead, without having to multiply the number by 100 and using the format string {0:N0}
.
(Background: In ASP.NET I have a GridView with a BoundField where I would like to display a number as percentage but without the percentage sign (%). How do I do that?)
Thanks for the answers. At the time of editing 4 out of 6 suggest what I would like to avoid, as explained above. I was looking for a way to use a format string only, and avoid multiplying by 100 and using {0:N0}
, but the answers indicate that's impossible...
Solved by using the accepted solution by Richard:
public class MyCulture : CultureInfo { public MyCulture() : base(Thread.CurrentThread.CurrentCulture.Name) { this.NumberFormat.PercentSymbol = ""; } } Thread.CurrentThread.CurrentCulture = new MyCulture();
Click the Number tab on the Format Cells window. Click the "General" listing in the Category pane. This action will remove the percentage signs and display the number without any formatting.
TO REMOVE THE % SIGN FROM A COLUMN, ROW, OR BLOCK OF NUMBERS: 1) Highlight the %numbers, then change their format to General (Format-Cells-General). 6) When you hit Enter, all your highlighted numbers will be changed to the proper VALUE without the percent SIGN.
Display numbers as percentagesOn the Home tab, in the Number group, click the icon next to Number to display the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Percentage. In the Decimal places box, enter the number of decimal places that you want to display.
Supposing you need to add a percentage symbol for a number, most of time you may select this cell first, and then click the button under Home tab, or right click > Format cells > Percentage > OK. By doing this, it won't only add a percentage sign to the number, but also to multiply the number by 100.
Define a custom culture with its own NumberFormatInfo
which returns String.Empty
for its PercentSymbol
property.
Then use that custom culture for impacted pages (or for the whole application). This could be done by cloning from the default so other regional settings are preserved.
Why don't you just multiply the number by 100 and use your "{0:N0}"
format string? That seems to me to be the easiest solution.
Unless you can come up with a viable reason why that's out of the question, that's my advice. It's not rocket science :-)
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