简体   繁体   中英

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

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 %")

But I don't want to display the decimal if not needed => "100 %" instead of "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 . See here on 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".

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

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. if you want a period independent of culture you need to quote it – with single quotes – eg. value.ToString("#'.'##") .

Standard Numeric Format Strings

"P" or "p" (Percent):

  • Result: Number multiplied by 100 and displayed with a percent symbol.
  • Supported by: All numeric types.
  • Precision specifier: Desired number of decimal places.
  • Default precision specifier: Defined by NumberFormatInfo.PercentDecimalDigits.

More information: The Percent ("P") Format Specifier.

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

NumberFormatInfo.PercentDecimalDigits contains this example:

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

Ok thanks, so that's not possible with 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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