如何格式化一个数字作为百分比没有百分号? [英] How to format a number as percentage without the percentage sign?

查看:186
本文介绍了如何格式化一个数字作为百分比没有百分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在.NET格式的数字为百分数,而不显示百分号?



如果我有号 0.13 ,并使用格式字符串 {0:P0} 输出 13%



不过,我想获得 13 来代替,的没有的有100乘以数量和使用格式字符串 {0:N0}



(背景:在ASP.NET我有一个的GridView绑定列在这里我想显示一个数字作为百分比,但没有百分号(%)。我该怎么办呢?)






感谢您的答案。在编辑时4出6建议我想什么来避免,如上所述。我一直在寻找一种方式来只使用一个格式字符串,并避免乘以100,并使用 {0:N0} ,但答案表明这是不可能的...






使用接受的解决方案

  
公共类MyCulture:CultureInfo的
{
公共MyCulture()
:基地(Thread.CurrentThread.CurrentCulture.Name)
{
本。 NumberFormat.PercentSymbol =;
}
}

Thread.CurrentThread.CurrentCulture =新MyCulture();


解决方案

定义一个定制的文化有自己的 的NumberFormatInfo 返回的String.Empty PercentSymbol 属性。



然后使用该定制文化的影响页面(或整个应用程序)。这可以通过默认设置,以便其他区域设置将被保留克隆来完成。


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();

解决方案

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.

这篇关于如何格式化一个数字作为百分比没有百分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆