Format decimal for percentage values?
Asked Answered
R

7

260

What I want is something like this:

String.Format("Value: {0:%%}.", 0.8526)

Where %% is that format provider or whatever I am looking for. Should result: Value: %85.26..

I basically need it for wpf binding, but first let's solve the general formatting issue:

<TextBlock Text="{Binding Percent, StringFormat=%%}" />
Roundabout answered 24/11, 2009 at 15:54 Comment(0)
O
497

Use the P format string. This will vary by culture:

String.Format("Value: {0:P2}.", 0.8526) // formats as 85.26 % (varies by culture)
Og answered 24/11, 2009 at 15:56 Comment(3)
Looking at here big difference is like US and France type. If above varies by culture, is there a culture-independent P formatting?Allare
@bonCodigo: if you want the output for a specific culture, specify the culture explicitly.Og
So how do I format a percent value in culture sensitive form but with a sign so that 0.123 is formatted as "+12.3%" and in tr_TR locale as "+%12.3"?Ischia
B
21

If you have a good reason to set aside culture-dependent formatting and get explicit control over whether or not there's a space between the value and the "%", and whether the "%" is leading or trailing, you can use NumberFormatInfo's PercentPositivePattern and PercentNegativePattern properties.

For example, to get a decimal value with a trailing "%" and no space between the value and the "%":

myValue.ToString("P2", new NumberFormatInfo { PercentPositivePattern = 1, PercentNegativePattern = 1 });

More complete example:

using System.Globalization; 

...

decimal myValue = -0.123m;
NumberFormatInfo percentageFormat = new NumberFormatInfo { PercentPositivePattern = 1, PercentNegativePattern = 1 };
string formattedValue = myValue.ToString("P2", percentageFormat); // "-12.30%" (in en-us)
Binford answered 8/6, 2015 at 20:20 Comment(1)
Or just do .ToString("0.00%") or .ToString("%0.00")Mycobacterium
M
12

Set your culture and "P" string format.

CultureInfo ci = new CultureInfo("en-us");
double floating = 72.948615;

Console.WriteLine("P02: {0}", (floating/100).ToString("P02", ci)); 
Console.WriteLine("P01: {0}", (floating/100).ToString("P01", ci)); 
Console.WriteLine("P: {0}", (floating/100).ToString("P", ci)); 
Console.WriteLine("P0: {0}", (floating/100).ToString("P0", ci));
Console.WriteLine("P1: {0}", (floating/100).ToString("P1", ci));
Console.WriteLine("P3: {0}", (floating/100).ToString("P3", ci));

Output:

"P02: 72.95%"

"P01: 72.9%"

"P: 72.95%"

"P0: 72%"

"P1: 72.9%"

"P3: 72.949%"

Merchantman answered 31/3, 2021 at 7:55 Comment(0)
P
10

If you want to use a format that allows you to keep the number like your entry this format works for me: "# \\%"

Pachisi answered 27/7, 2018 at 22:53 Comment(0)
A
7

This code may help you:

double d = double.Parse(input_value);
string output= d.ToString("F2", CultureInfo.InvariantCulture) + "%";
Antidote answered 22/6, 2019 at 10:6 Comment(1)
The worked for me without the Culture.InvariantCultureHairpiece
M
6

There's a simple and culture-independent approach: just use the "%" custom specifier and manually control the sign position. https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#SpecifierPct

A percent sign (%) in a format string causes a number to be multiplied by 100 before it is formatted. The localized percent symbol is inserted in the number at the location where the % appears in the format string.

string.Format("{0:0.0%}", 0.6493072393590115)
// outputs 64.9%

string.Format("{0:%000}", 0.6493072393590115)
// outputs %065
Marnimarnia answered 26/7, 2022 at 13:53 Comment(0)
D
-9

I have found the above answer to be the best solution, but I don't like the leading space before the percent sign. I have seen somewhat complicated solutions, but I just use this Replace addition to the answer above instead of using other rounding solutions.

String.Format("Value: {0:P2}.", 0.8526).Replace(" %","%") // formats as 85.26% (varies by culture)
Disconsolate answered 2/12, 2015 at 14:34 Comment(1)
And still wrong, if you want to force that input so much you could put the number as float and add the percent sign, since replace is costly and in this case not very useful "String.Format("Value: {0:F2}.", 0.8526*100)"Miran

© 2022 - 2024 — McMap. All rights reserved.