Original solution
Basically this is what you want to do:
string.Format("{0:0.############## %}", 0.1234);
string.Format("{0:0.############## %}", 0.1234567);
Note: for decimal you would use 0.############################.
disadvantages of alternatives
, 0:0.00000000000000 % :
string.Format("{0:0.00000000000000 %}", 0.1234);
P14 NumberFormatInfo.PercentDecimalDigits . , . , .
, 0.############## %.
( )
, P, :
public string GetPercentFormat(CultureInfo culture)
{
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
NumberFormatInfo nfi = culture.NumberFormat;
var baseFormat = "0.################";
var percentSymbol = "%";
var negativeSign = nfi.NegativeSign;
string customFormat = string.Empty;
switch (nfi.PercentPositivePattern)
{
case 1:
customFormat += baseFormat + percentSymbol;
break;
case 2:
customFormat += percentSymbol + baseFormat;
break;
case 3:
customFormat += percentSymbol + " " + baseFormat;
break;
case 0:
default:
customFormat += baseFormat + " " + percentSymbol;
break;
}
customFormat += ";";
switch (nfi.PercentNegativePattern)
{
case 1:
customFormat += negativeSign + baseFormat + percentSymbol;
break;
case 2:
customFormat += negativeSign + percentSymbol + baseFormat;
break;
case 3:
customFormat += percentSymbol + negativeSign + baseFormat;
break;
case 4:
customFormat += percentSymbol + baseFormat + negativeSign;
break;
case 5:
customFormat += baseFormat + negativeSign + percentSymbol;
break;
case 6:
customFormat += baseFormat + percentSymbol + negativeSign;
break;
case 7:
customFormat += negativeSign + percentSymbol + " " + baseFormat;
break;
case 8:
customFormat += baseFormat + " " + percentSymbol + negativeSign;
break;
case 9:
customFormat += percentSymbol + " " + baseFormat + negativeSign;
break;
case 10:
customFormat += percentSymbol + " " + negativeSign + baseFormat;
break;
case 11:
customFormat += baseFormat + negativeSign + " " + percentSymbol;
break;
case 0:
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");
var customFormat = GetPercentFormat(culture);
Console.WriteLine(string.Format(culture, "{0:"+ customFormat + "}", 0.1234));
}
:
%12,34
GetPercentFormat . CultureInfo. : , (NumberFormatInfo.PercentGroupSeparator) (NumberFormatInfo.PercentGroupSizes) .