简体   繁体   English

如何在C#中以小数点显示百分比并使用string.Format管理区域性?

[英]How to display percentage with one decimal and manage culture with string.Format in C#?

I would like to display a percentage and manage the culture. 我想展示一个百分比并管理文化。 Like this : https://msdn.microsoft.com/fr-fr/library/system.globalization.numberformatinfo.percentnegativepattern%28v=vs.110%29.aspx 像这样: https : //msdn.microsoft.com/fr-fr/library/system.globalization.numberformatinfo.percentnegativepattern%28v=vs.110%29.aspx

I do that : 我这样做:

double percentage = 0.239;
NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
string percentageValue = string.Format(nfi, "{0:P1}", percentage);

It works (for example result can be "%23,9" or "23,9 %") 它有效(例如,结果可以是“%23,9”或“ 23,9%”)

But I don't want to display the decimal if not needed => "100 %" instead of "100,0 %". 但是我不想显示小数,如果不需要=>“ 100%”而不是“ 100,0%”。

I try with #.#, its works but I want to manage the current culture (decimal separator, percentage position, etc). 我尝试使用#。#,它的工作原理,但是我想管理当前的区域性(小数点分隔符,百分比位置等)。

How can I achieve that ? 我该如何实现?

Thanks ! 谢谢 !

A period ( . ) in the format is actually a replaced character: the culture's decimal separator 1 . 格式中的句点( . )实际上是一个替换字符:文化的小数点分隔符1 See here on MSDN. 请参见MSDN上的此处

So that part is easy. 因此,这部分很容易。

However the P format's decimal places are based on details in the applicable locale, there is no custom formatting for "percent digits". 但是, P格式的小数位基于适用语言环境中的详细信息,没有针对“百分比数字”的自定义格式。

Additionally 另外

But I don't want to display the decimal if not neede 但如果不需要,我不想显示小数

is very hard for floating point values. 对于浮点值非常困难。 As approximations any attempt at something like if (value.FractionalPart == 0) is doomed the underlying binary representation. 作为近似值,对if (value.FractionalPart == 0)类的任何尝试注定了基础二进制表示形式。 For example 0.1 (10%) is not represented exactly and after multiplying by 100 (for the percentage display) is unlikely to be exactly 10. Thus "has no decimal places" will actually need to be "sufficiently close to an integral value": 例如,未精确表示0.1(10%),并且乘以100(对于百分比显示)后不可能恰好是10。因此,“无小数位”实际上需要“足够接近整数值”:

var hasFraction = Math.Abs(value*100.0 - Math.Round(value*100, 0)) < closeEnough;

and then build the format string depending on the result. 然后根据结果构建格式字符串。


1 Ie. 1点 if you want a period independent of culture you need to quote it – with single quotes – eg. 如果您想要一个不受文化影响的时期,则需要用单引号将其引用,例如。 value.ToString("#'.'##") . value.ToString("#'.'##")

Standard Numeric Format Strings 标准数字格式字符串

"P" or "p" (Percent): “ P”或“ p”(百分比):

  • Result: Number multiplied by 100 and displayed with a percent symbol. 结果:数字乘以100,并显示一个百分号。
  • Supported by: All numeric types. 支持的对象:所有数字类型。
  • Precision specifier: Desired number of decimal places. 精度说明符:所需的小数位数。
  • Default precision specifier: Defined by NumberFormatInfo.PercentDecimalDigits. 默认精度说明符:由NumberFormatInfo.PercentDecimalDigits定义。

More information: The Percent ("P") Format Specifier. 详细信息:百分比(“ P”)格式说明符。

  • 1 ("P", en-US) -> 100.00 % 1(“ P”,美国)-> 100.00%
  • 1 ("P", fr-FR) -> 100,00 % 1(“ P”,fr-FR)-> 100,00%
  • -0.39678 ("P1", en-US) -> -39.7 % -0.39678(“ P1”,美国)-> -39.7%
  • -0.39678 ("P1", fr-FR) -> -39,7 % -0.39678(“ P1”,fr-FR)-> -39,7%

NumberFormatInfo.PercentDecimalDigits contains this example: NumberFormatInfo.PercentDecimalDigits包含以下示例:

NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;

// Displays a negative value with the default number of decimal digits (2).
Double myInt = 0.1234;
Console.WriteLine( myInt.ToString( "P", nfi ) );

// Displays the same value with four decimal digits.
nfi.PercentDecimalDigits = 4;
Console.WriteLine( myInt.ToString( "P", nfi ) );

Which results in the output: 结果为输出:

  • 12.34 % 12.34%
  • 12.3400 % 12.3400%

Ok thanks, so that's not possible with string.Format() 好的,谢谢,所以这不可能用string.Format()

And what do you think of this ? 您对此有何看法?

bool hasDecimal = !percentage.Value.ToString("P1", CultureInfo.InvariantCulture).EndsWith(".0 %");
string percentageMask = hasDecimal ? "{0:P1}" : "{0:P0}";
string percentageValue = string.Format(CultureInfo.CurrentCulture, percentageMask, percentage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM