我看到了一个有趣的情况,四舍五入货币在C(与2008年SP1)。下面是测试用例的图像:
alt text http://img697.imageshack.us/img697/8500/testcases.png
我原以为第五、六和七种情况(我不擅长在输出中对它们进行编号)会把数字四舍五入到一便士。
这是我的测试代码:
static void Main(string[] args)
{
decimal one = 10.994m;
decimal two = 10.995m;
decimal three = 1.009m;
decimal four = 0.0044m;
decimal five = 0.0045m;
decimal six = 0.0046m;
decimal seven = 0.0049m;
decimal eight = 0.0050m;
Console.WriteLine(one + ": " + one.ToString("C"));
Console.WriteLine(two + ": " + two.ToString("C"));
Console.WriteLine(three + ": " + three.ToString("C"));
Console.WriteLine(four + ": " + four.ToString("C"));
Console.WriteLine(five + ": " + five.ToString("C"));
Console.WriteLine(six + ": " + six.ToString("C"));
Console.WriteLine(seven + ": " + seven.ToString("C"));
Console.WriteLine(eight + ": " + eight.ToString("C"));
Console.ReadLine();
}
当我在.to string(字符串格式)中查看发生了什么时,我发现
public string ToString(string format)
{
return Number.FormatDecimal(this, format, NumberFormatInfo.CurrentInfo);
}
它的使命是
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string FormatDecimal(
decimal value,
string format,
NumberFormatInfo info);
这个调用中是否有逻辑表明numberformatinfo的当前区域性设置的粒度是currencity的两位小数,所以不要让第10000位的数字上卷,因为它无关紧要?
这个方法是如何实现的?我们是进入了比特转换区,还是发生了其他事情?
谢谢你的洞察力。