Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

c# - How to format a number as percentage without the percentage sign?

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...