如何将数字格式化为不带百分号的百分比?

发布于 2024-07-23 03:45:31 字数 778 浏览 3 评论 0原文

如何在 .NET 中将数字格式化为百分比而不显示百分号?

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

不过,我希望得到 13无需将数字乘以 100 并使用格式字符串 {0:N0}

(背景:在 ASP.NET 中,我有一个带有 BoundField 的 GridView,我想在其中将数字显示为百分比,但不带百分号 (%)。我该怎么做?)


感谢您的回答。 在编辑时,六分之四提出了我想避免的内容,如上所述。 我一直在寻找一种仅使用格式字符串的方法,并避免乘以 100 并使用 {0:N0},但答案表明这是不可能的...


通过使用 < 接受的解决方案来解决a href="https://stackoverflow.com/users/67392/richard">理查德:


public class MyCulture : CultureInfo
{
    public MyCulture()
        : base(Thread.CurrentThread.CurrentCulture.Name)
    {
        this.NumberFormat.PercentSymbol = "";
    }
}

Thread.CurrentThread.CurrentCulture = new MyCulture();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

浪漫之都 2024-07-30 03:45:31

使用其自己的 NumberFormatInfo 定义自定义区域性,该区域性返回String.Empty 为其 PercentSymbol 属性。

然后对受影响的页面(或整个应用程序)使用该自定义区域性。 这可以通过从默认值进行克隆来完成,以便保留其他区域设置。

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.

不念旧人 2024-07-30 03:45:31

为什么不直接将数字乘以 100 并使用 "{0:N0}" 格式字符串? 在我看来,这是最简单的解决方案。

除非你能找出一个可行的理由来说明为什么这是不可能的,否则这就是我的建议。 这不是火箭科学 :-)

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

顾北清歌寒 2024-07-30 03:45:31

但乘以 100 正是您想要的!

protected void myGrdiView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        myObjectType ot = (myObjectType)e.Row.DataItem;

        ot.myNumber = ot.myNumber * 100; // multiply by 100
    }
}

并在 HTML 中

<asp:BoundField DataType="myNumber" HeaderText="%" StringFormat="{0:N0}" />

but multiplying by 100 is exactly what you want!

protected void myGrdiView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        myObjectType ot = (myObjectType)e.Row.DataItem;

        ot.myNumber = ot.myNumber * 100; // multiply by 100
    }
}

and in the HTML

<asp:BoundField DataType="myNumber" HeaderText="%" StringFormat="{0:N0}" />
陌路黄昏 2024-07-30 03:45:31

MSDN* 在“自定义数字格式字符串”下有此内容:

a 中存在“%”字符
格式字符串导致数字成为
之前乘以100
格式化。 适当的符号是
插入数字本身
'%' 出现的位置
格式字符串。 百分比字符
使用取决于当前
NumberFormatInfo 类。

但该示例显示它还输出 % 符号 - 这不是您想要的,但也许可以通过 NumberFormatInfo 类设置为空?

但是,我同意 Pax 的观点,并且不明白为什么不使用 * 100 和 {0:N0}

**从 Visual Studio 内访问,因此没有链接*

The MSDN* has this under "Custom Numeric Format Strings":

The presence of a '%' character in a
format string causes a number to be
multiplied by 100 before it is
formatted. The appropriate symbol is
inserted in the number itself at the
location where the '%' appears in the
format string. The percent character
used is dependent on the current
NumberFormatInfo class.

But the example shows that it also outputs the % sign - not what you want, but perhaps settable to nothing via the NumberFormatInfo class?

However, I agree with Pax and can't see why do don't go with the * 100 and {0:N0}

**Accessing from within Visual Studio so no link*

ぶ宁プ宁ぶ 2024-07-30 03:45:31

这个怎么样...

String.Format("{0:P0}",0.13).Replace("%","")

编辑:这应该适用于跨文化:

var percentSymbol = CultureInfo.CurrentCulture.NumberFormat.PercentSymbol;
String.Format("{0:P0}",0.13).Replace(percentSymbol,"")

还有这个解决方案可能更优雅,但代码稍微多一些。

How about this...

String.Format("{0:P0}",0.13).Replace("%","")

EDIT: This should work across cultures:

var percentSymbol = CultureInfo.CurrentCulture.NumberFormat.PercentSymbol;
String.Format("{0:P0}",0.13).Replace(percentSymbol,"")

There is also this solution which may be more elegant but slightly more code.

盗心人 2024-07-30 03:45:31

修剪一下怎么样?

double d = .102;
string percent = d.ToString("P0").Trim(' ', '%');

How about a trim?

double d = .102;
string percent = d.ToString("P0").Trim(' ', '%');
再浓的妆也掩不了殇 2024-07-30 03:45:31

首先要考虑的一点是,没有%的百分比显示是一个数字,所以让我们忽略百分比方面。 您想知道如何将 1 或更少的数字显示为 100 或更少。 我明白它是绑定的,因此您无法在显示时修改它,那么为什么不在查询时修改它,即 SELECT (value*100) AS Percentage,...?

A points to consider first, a percentage displayed without a % is a number so lets ignore the percentage aspect. You want to know how to display a number that's 1 or less as 100 or less. I appreciate that it's bound so you can't modify it at display time so why not modify it at query time, i.e. SELECT (value*100) AS Percentage, ...?

聚集的泪 2024-07-30 03:45:31

你也可以尝试类似的东西

string newString = "0.13".Replace("0.", string.Empty) + "%";

You could also try something like

string newString = "0.13".Replace("0.", string.Empty) + "%";
梦中楼上月下 2024-07-30 03:45:31

这是一个使用 NumberFormatInfo 作为 @Richard 建议的示例:

string.Format(new NumberFormatInfo { PercentSymbol = string.Empty }, "{0:0%}", 0.123); // => 12

Here is an example using NumberFormatInfo as @Richard suggested:

string.Format(new NumberFormatInfo { PercentSymbol = string.Empty }, "{0:0%}", 0.123); // => 12
烏雲後面有陽光 2024-07-30 03:45:31

这是另一种更短、更简洁的方法。

$"{rate * 100:F2}"

Here is another shorter and cleaner way to do it.

$"{rate * 100:F2}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文