C# 格式化货币
我看到 C# 中的货币四舍五入的有趣情况(VS 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();
}
当我反映到 .ToString(string format) 以查看发生了什么时,我发现
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);
Is There somelogic in that call 表明我当前的 NumberFormatInfo 区域性设置的粒度是两位小数货币的位数,所以不要让千分之十位将数字累加起来,因为它微不足道?
这个方法是如何实现的呢?我们是进入了位移领域,还是发生了其他事情?
感谢您的任何见解。
I am seeing an intriguing situation rounding Currency in C# (VS 2008 SP1). Below is an image of the test cases:
alt text http://img697.imageshack.us/img697/8500/testcases.png
I was expecting cases five, six, and seven (my bad on not numbering them in the output) to round the number up to a penny.
Here is my test code:
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();
}
When I reflected into .ToString(string format) to see what was going on I found
public string ToString(string format)
{
return Number.FormatDecimal(this, format, NumberFormatInfo.CurrentInfo);
}
which has the call to
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string FormatDecimal(
decimal value,
string format,
NumberFormatInfo info);
Is there some logic in that call that says the granularity for my current culture settings for NumberFormatInfo is two decimal places for currencty so don't let the ten thousandths place roll the number up because it is insignificant?
How is this method implemented? Are we into bit shift land, or is something else going on?
Thanks for any insights.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据基本数学原理,情况 4、5、6 和 7 不应四舍五入为一便士。您不会通过从最右边的数字开始向上舍入来进行舍入。您只需查看要舍入的数字右侧的一位数字。
http://www.enchantedlearning.com/math/rounding/
计算机仅执行基本操作数学,正如他们应该做的那样。
编辑 - 添加
更好的链接:
http://math.about.com/od/arithmetic/a/Rounding.htm
Following basic mathematical principles, cases 4, 5, 6, and 7 should not round up to a penny. You don't round by starting at the right-most number and rounding up. You only look one digit to the right of the number you want to round to.
http://www.enchantedlearning.com/math/rounding/
the computer is just performing basic math, as they are supposed to.
Edit - added
a better link:
http://math.about.com/od/arithmetic/a/Rounding.htm