Percentage double format - show all decimal numbers?

I cannot find a way to format a double as a percentage showing all decimal numbers in C # 4.0.

Use {0:P}has the same effect as {0:P2}, i.e. percentage formatting, strict display of two decimal places.

Finally, I went to replace the problem {0:P}with {0:0.################################%}. For my purpose, my doubles do not reach these decimal places.

+4
source share
5 answers

Original solution

Basically this is what you want to do:

string.Format("{0:0.############## %}", 0.1234); // 12.34 %
string.Format("{0:0.############## %}", 0.1234567); //12.34567 %

Note: for decimal you would use 0.############################.


disadvantages of alternatives

, 0:0.00000000000000 % :

string.Format("{0:0.00000000000000 %}", 0.1234); // 12.34000000000000 %

P14 NumberFormatInfo.PercentDecimalDigits . , . , .

, 0.############## %.


( )

, P, :

public string GetPercentFormat(CultureInfo culture)
{
    if (culture == null)
    {
        culture = CultureInfo.CurrentCulture;
    }
    NumberFormatInfo nfi = culture.NumberFormat;
    //% and . are localized by default
    var baseFormat = "0.################";
    var percentSymbol = "%";
    var negativeSign = nfi.NegativeSign; //It may be problematic
    string customFormat = string.Empty;
    switch (nfi.PercentPositivePattern)
    {
        case 1: //n%
            customFormat += baseFormat + percentSymbol;
            break;
        case 2: //%n
            customFormat += percentSymbol + baseFormat;
            break;
        case 3: //% n
            customFormat += percentSymbol + " " + baseFormat;
            break;
        case 0: //n %
        default:
            customFormat += baseFormat + " " + percentSymbol;
            break;
    }
    customFormat += ";";
    switch (nfi.PercentNegativePattern)
    {
        case 1: //-n%
            customFormat += negativeSign + baseFormat + percentSymbol;
            break;
        case 2: //-%n
            customFormat += negativeSign + percentSymbol + baseFormat;
            break;
        case 3: //%-n
            customFormat += percentSymbol + negativeSign + baseFormat;
            break;
        case 4: //%n-
            customFormat += percentSymbol + baseFormat + negativeSign;
            break;
        case 5: //n-%
            customFormat += baseFormat + negativeSign + percentSymbol;
            break;
        case 6: //n%-
            customFormat += baseFormat + percentSymbol + negativeSign;
            break;
        case 7: //-% n
            customFormat += negativeSign + percentSymbol + " " + baseFormat;
            break;
        case 8: //n %-
            customFormat += baseFormat + " " + percentSymbol + negativeSign;
            break;
        case 9: //% n-
            customFormat += percentSymbol + " " + baseFormat + negativeSign;
            break;
        case 10: //% -n
            customFormat += percentSymbol + " " + negativeSign + baseFormat;
            break;
        case 11: //n- %
            customFormat += baseFormat + negativeSign + " " + percentSymbol;
            break;
        case 0: //-n %
        default:
            customFormat += negativeSign + baseFormat + " " + percentSymbol;
            break;
    }
    return customFormat;
}

: StringBuilder.

:

void Main()
{
    var customFormat = GetPercentFormat(null);
    Console.WriteLine(string.Format("{0:"+ customFormat + "}", 0.1234));
}

:

12.34 %

CultureInfo:

void Main()
{
    var culture = new CultureInfo("quz-BO"); //Quechua (Bolivia)
    var customFormat = GetPercentFormat(culture);
    Console.WriteLine(string.Format(culture, "{0:"+ customFormat + "}", 0.1234));
}

:

%12,34

GetPercentFormat . CultureInfo. : , (NumberFormatInfo.PercentGroupSeparator) (NumberFormatInfo.PercentGroupSizes) .

+1

CultureInfo.InvariantCulture , ,

, :

double myDouble = (double)1 / 3; // 0.3333333...
string asString = string.Format("{0} %", myDouble * 100);
+1

"P" , , NumberFormatInfo PercentDecimalDigits.

NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
Console.WriteLine(string.Format("{0} PercentDecimalDigits", nfi.PercentDecimalDigits)); 

, :

Console.WriteLine(string.Format("Value: {0:P4}.", 0.123456)); 

.

0

{0: P}

, .

, ,

double valueToDisplay = (10.0/7.0); var percentValue = String.Format( "{0}%", valueToDisplay);

0
source

All Articles