在逗号后使用自定义位数格式化十进制数
Format decimal number with custom amount of digits after comma
我有这个自定义扩展,它应该在逗号后使用自定义位数格式化十进制数。
public static decimal FormatDecimal(this decimal value, int decimalSeparator = 2)
{
decimal returnValue = Math.Round(value, decimalSeparator, MidpointRounding.AwayFromZero);
return returnValue;
}
问题是没有按预期工作。
如果我这样做:
decimal number = 12345;
然后:
decimal formatedNumber = number.FormatDecimal(2);
结果应该是:
12345.00
结果是:
12345
我做错了什么?
您可能正在寻找格式化小数的字符串表示形式。试试这个:
decimal myNumber = 12345.67m;
string formattedNumber = myNumber.ToString("N3");
Console.WriteLine(formattedNumber); // Prints "12345.670"
查看此处了解更多信息:https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx
来自 MSDN:
Standard numeric format strings are used to format common numeric types. A standard numeric format string takes the form Axx, where:
A is a single alphabetic character called the format specifier. Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string. For more information, see Custom Numeric Format Strings.
xx is an optional integer called the precision specifier. The precision specifier ranges from 0 to 99 and affects the number of digits in the result. Note that the precision specifier controls the number of digits in the string representation of a number. It does not round the number itself. To perform a rounding operation, use the Math.Ceiling, Math.Floor, or Math.Round method.
When precision specifier controls the number of fractional digits in the result string, the result strings reflect numbers that are rounded away from zero (that is, using MidpointRounding.AwayFromZero).
我认为正确的方法是使用 The "0"
custom format specifier;
Replaces the zero with the corresponding digit if one is present;
otherwise, zero appears in the result string.
例如;
decimal d = 12345;
Console.WriteLine(d.ToString("#.00")); // 12345.00
您应该在显示字符串时指定格式。
当你转换为字符串时,你需要做的是:
String.Format("{0:0.00}", formatedNumber);
参考This article了解更多详情:
这是扩展功能的工作
public static string FormatDecimal(this decimal value, int decimalSeparator = 2)
{
return value.ToString(string.Format("0.{0}", new string('0', decimalSeparator)));
}
我有这个自定义扩展,它应该在逗号后使用自定义位数格式化十进制数。
public static decimal FormatDecimal(this decimal value, int decimalSeparator = 2)
{
decimal returnValue = Math.Round(value, decimalSeparator, MidpointRounding.AwayFromZero);
return returnValue;
}
问题是没有按预期工作。
如果我这样做:
decimal number = 12345;
然后:
decimal formatedNumber = number.FormatDecimal(2);
结果应该是:
12345.00
结果是:
12345
我做错了什么?
您可能正在寻找格式化小数的字符串表示形式。试试这个:
decimal myNumber = 12345.67m;
string formattedNumber = myNumber.ToString("N3");
Console.WriteLine(formattedNumber); // Prints "12345.670"
查看此处了解更多信息:https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx
来自 MSDN:
Standard numeric format strings are used to format common numeric types. A standard numeric format string takes the form Axx, where:
A is a single alphabetic character called the format specifier. Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string. For more information, see Custom Numeric Format Strings.
xx is an optional integer called the precision specifier. The precision specifier ranges from 0 to 99 and affects the number of digits in the result. Note that the precision specifier controls the number of digits in the string representation of a number. It does not round the number itself. To perform a rounding operation, use the Math.Ceiling, Math.Floor, or Math.Round method.
When precision specifier controls the number of fractional digits in the result string, the result strings reflect numbers that are rounded away from zero (that is, using MidpointRounding.AwayFromZero).
我认为正确的方法是使用 The "0"
custom format specifier;
Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
例如;
decimal d = 12345;
Console.WriteLine(d.ToString("#.00")); // 12345.00
您应该在显示字符串时指定格式。
当你转换为字符串时,你需要做的是:
String.Format("{0:0.00}", formatedNumber);
参考This article了解更多详情:
这是扩展功能的工作
public static string FormatDecimal(this decimal value, int decimalSeparator = 2)
{
return value.ToString(string.Format("0.{0}", new string('0', decimalSeparator)));
}