C# 格式化货币

发布于 2024-08-28 02:14:49 字数 1619 浏览 6 评论 0原文

我看到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2024-09-04 02:14:49

根据基本数学原理,情况 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文