如何将数字格式化为不带百分号的百分比?
如何在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用其自己的
NumberFormatInfo
定义自定义区域性,该区域性返回String.Empty
为其PercentSymbol
属性。然后对受影响的页面(或整个应用程序)使用该自定义区域性。 这可以通过从默认值进行克隆来完成,以便保留其他区域设置。
Define a custom culture with its own
NumberFormatInfo
which returnsString.Empty
for itsPercentSymbol
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.
为什么不直接将数字乘以 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 :-)
但乘以 100 正是您想要的!
并在 HTML 中
but multiplying by 100 is exactly what you want!
and in the HTML
MSDN* 在“自定义数字格式字符串”下有此内容:
但该示例显示它还输出 % 符号 - 这不是您想要的,但也许可以通过 NumberFormatInfo 类设置为空?
但是,我同意 Pax 的观点,并且不明白为什么不使用 * 100 和 {0:N0}
**从 Visual Studio 内访问,因此没有链接*
The MSDN* has this under "Custom Numeric Format Strings":
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*
这个怎么样...
编辑:这应该适用于跨文化:
还有这个解决方案可能更优雅,但代码稍微多一些。
How about this...
EDIT: This should work across cultures:
There is also this solution which may be more elegant but slightly more code.
修剪一下怎么样?
How about a trim?
首先要考虑的一点是,没有%的百分比显示是一个数字,所以让我们忽略百分比方面。 您想知道如何将 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, ...?
你也可以尝试类似的东西
You could also try something like
这是一个使用 NumberFormatInfo 作为 @Richard 建议的示例:
Here is an example using NumberFormatInfo as @Richard suggested:
这是另一种更短、更简洁的方法。
Here is another shorter and cleaner way to do it.