C# 如何将数字格式化为没有百分比符号的百分比?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/939132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:47:57  来源:igfitidea点击:

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

c#string-formatting

提问by Ole Lynge

How do I in .NET format a number as percentage without showing the percentage sign?

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

If I have the number 0.13and use the format string {0:P0}the output is 13 %.

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

However I would like to get 13instead, withouthaving to multiply the number by 100 and using the format string {0:N0}.

但是我想得到13而不必将数字乘以 100 并使用格式 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?)

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



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...

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



Solved by using the accepted solution by Richard:

通过使用Richard接受的解决方案解决:


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

Thread.CurrentThread.CurrentCulture = new MyCulture();

采纳答案by Richard

Define a custom culture with its own NumberFormatInfowhich returns String.Emptyfor its PercentSymbolproperty.

用它自己的定义一个自定义文化,它NumberFormatInfo返回String.Empty它的PercentSymbol属性。

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.

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

回答by paxdiablo

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.

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

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

除非你能提出一个可行的理由,否则这是我的建议。这不是火箭科学 :-)

回答by ChrisF

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

MSDN* 在“自定义数字格式字符串”下有这个:

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.

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

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

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

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

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

**Accessing from within Visual Studio so no link*

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

回答by HitLikeAHammer

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 solutionwhich may be more elegant but slightly more code.

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

回答by balexandre

but multiplying by 100 is exactly what you want!

但乘以 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
    }
}

and in the HTML

并在 HTML 中

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

回答by Colin Burnett

How about a trim?

修剪一下怎么样?

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

回答by Lazarus

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, ...?

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

回答by mtranda

You could also try something like

你也可以尝试类似的东西

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

回答by Robin

Here is an example using NumberFormatInfo as @Richard suggested:

以下是@Richard 建议使用 NumberFormatInfo 的示例:

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

回答by karmjit singh

Here is another shorter and cleaner way to do it.

这是另一种更短更干净的方法。

$"{rate * 100:F2}"