String.Format 具有无限精度和至少 2 个小数位
String.Format with infinite precision and at least 2 decimal digit
我正在为一些对数字准确性非常挑剔的人开发一个应用程序(他们正在处理会计和非常准确的电信变量)。出于这个原因,我到处都使用 decimal
类型,因为 float 和 double 类型不合适(请不要犹豫,将我重定向到现有的更好的数据类型)。
当我需要格式化这些数字时,我的问题就来了。要求是根据需要显示尽可能多的小数位,但至少要显示 2 位,并且还要使用千位组分隔符。
例如:
Value Formatted
1 1.00
1000 1,000.00
1.5 1.50
1000.355 1,000.355
0.000035 0.000035
所以我去 MSDN 寻找数字字符串格式。我发现这个有用的资源 Standard Numeric Formats 但我的尝试 none 按预期工作(N、N2、F、F2、G、G2 等,我尝试了各种组合,即使我不相信它们^^ - 我什至尝试了一些 F2# 来找乐子)。
我的结论是没有内置格式可以满足我的要求。 对吗?
所以我查看了下一章Custom Numeric Formats. But I couldn't find a combination that suit my needs. So I went to SO and find a lots of question about that (1, 2,依此类推。
这些问题让我担心唯一的解决方案是这个:#,##0.00#######
尾随 # 的数量与我需要的精度一样多。
我说的对吗?
我猜 12 #,我的人不会发现任何准确性问题,但我可能错过了我需要的神奇格式?
如果您想完全控制格式,您可以为小数实现自己的 IFormatProvider。在其中,您可以使用 StringBuilder 并在不受 string.Format().
限制的情况下做任何您需要的事情
这可能是您要查找的内容:
static string FormatNumber(string input)
{
var dec = decimal.Parse(input);
var bits = decimal.GetBits(dec);
var prec = bits[3] >> 16 & 255;
if (prec < 2)
prec = 2;
return dec.ToString("N" + prec);
}
调用它时,对小数执行 ToString()
,如果需要,将结果转换回小数。
我尝试了您的示例数字,结果:
基于.
我创建了一个小函数:它根据数字的小数位数格式化数字:
public static void Main()
{
decimal theValue;
theValue = 0.000035M;
Console.WriteLine(theFormat(theValue));
theValue = 1.5M;
Console.WriteLine(theFormat(theValue));
theValue = 1;
Console.WriteLine(theFormat(theValue));
}
public static decimal theFormat(decimal theValue){
int count = BitConverter.GetBytes(decimal.GetBits(theValue)[3])[2];
return count > 1?theValue:Convert.ToDecimal(string.Format("{0:F2}", theValue));
}
这会产生以下输出:
0.000035
1.50
1.00
我正在为一些对数字准确性非常挑剔的人开发一个应用程序(他们正在处理会计和非常准确的电信变量)。出于这个原因,我到处都使用 decimal
类型,因为 float 和 double 类型不合适(请不要犹豫,将我重定向到现有的更好的数据类型)。
当我需要格式化这些数字时,我的问题就来了。要求是根据需要显示尽可能多的小数位,但至少要显示 2 位,并且还要使用千位组分隔符。
例如:
Value Formatted
1 1.00
1000 1,000.00
1.5 1.50
1000.355 1,000.355
0.000035 0.000035
所以我去 MSDN 寻找数字字符串格式。我发现这个有用的资源 Standard Numeric Formats 但我的尝试 none 按预期工作(N、N2、F、F2、G、G2 等,我尝试了各种组合,即使我不相信它们^^ - 我什至尝试了一些 F2# 来找乐子)。
我的结论是没有内置格式可以满足我的要求。 对吗?
所以我查看了下一章Custom Numeric Formats. But I couldn't find a combination that suit my needs. So I went to SO and find a lots of question about that (1, 2,依此类推。
这些问题让我担心唯一的解决方案是这个:#,##0.00#######
尾随 # 的数量与我需要的精度一样多。
我说的对吗?
我猜 12 #,我的人不会发现任何准确性问题,但我可能错过了我需要的神奇格式?
如果您想完全控制格式,您可以为小数实现自己的 IFormatProvider。在其中,您可以使用 StringBuilder 并在不受 string.Format().
限制的情况下做任何您需要的事情这可能是您要查找的内容:
static string FormatNumber(string input)
{
var dec = decimal.Parse(input);
var bits = decimal.GetBits(dec);
var prec = bits[3] >> 16 & 255;
if (prec < 2)
prec = 2;
return dec.ToString("N" + prec);
}
调用它时,对小数执行 ToString()
,如果需要,将结果转换回小数。
我尝试了您的示例数字,结果:
基于
我创建了一个小函数:它根据数字的小数位数格式化数字:
public static void Main()
{
decimal theValue;
theValue = 0.000035M;
Console.WriteLine(theFormat(theValue));
theValue = 1.5M;
Console.WriteLine(theFormat(theValue));
theValue = 1;
Console.WriteLine(theFormat(theValue));
}
public static decimal theFormat(decimal theValue){
int count = BitConverter.GetBytes(decimal.GetBits(theValue)[3])[2];
return count > 1?theValue:Convert.ToDecimal(string.Format("{0:F2}", theValue));
}
这会产生以下输出:
0.000035
1.50
1.00