Format decimal for percentage values?

C#Decimalstring.formatFormatprovider

C# Problem Overview


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=%%}" />

C# Solutions


Solution 1 - C#

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

String.Format("Value: {0:P2}.", 0.8526) // formats as 85.26 % (varies by culture)

Solution 2 - C#

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)

Solution 3 - C#

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

Solution 4 - C#

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

"P1: 72.9%"

"P3: 72.949%"

Solution 5 - C#

This code may help you:

double d = double.Parse(input_value);
string output= d.ToString("F2", CultureInfo.InvariantCulture) + "%";

Solution 6 - C#

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShimmy WeitzhandlerView Question on Stackoverflow
Solution 1 - C#Michael HarenView Answer on Stackoverflow
Solution 2 - C#Jon SchneiderView Answer on Stackoverflow
Solution 3 - C#David NeiraView Answer on Stackoverflow
Solution 4 - C#suleymanduzgunView Answer on Stackoverflow
Solution 5 - C#Nitika ChopraView Answer on Stackoverflow
Solution 6 - C#Ian TownsendView Answer on Stackoverflow