Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a number as percentage without the percentage sign?

Tags:

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(); 
like image 335
Ole Lynge Avatar asked Jun 02 '09 12:06

Ole Lynge


People also ask

How do I get rid of the percentage sign in numbers?

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.

How do I get rid of the percent sign in Excel?

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.

How do you change a number into a percentage format?

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.

How do I add a percentage symbol in Excel without changing values?

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.


2 Answers

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.

like image 103
Richard Avatar answered Sep 16 '22 21:09

Richard


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

like image 41
paxdiablo Avatar answered Sep 20 '22 21:09

paxdiablo