在 Paypal 中进行货币转换四舍五入的正确方法?
我正在建立一个与贝宝集成的电子商务网站。
我们采用多种货币,所以我想确保(出于会计原因)我正确地执行了货币转换的任何数学运算。
将货币换算值乘以原始货币后,我总是会在小数点后得到很多尾随数字。
有没有标准的方法来做到这一点?我应该截断还是四舍五入?我是否需要多次舍入,以防舍入 1/1000 小数会影响舍入 1/100 小数?
我是否应该做类似的事情:
Math.Round(Math.Round(x, 3), 2)
我一直无法找到有关如何完成此操作的良好信息(希望美国和欧洲是相同的)。
I'm building an ecommerce site integrated with paypal.
We take multiple currencies, so I want to make sure that (for accounting reasons) i'm correctly performing any math for currency conversion.
After multiplying the currency conversion * the original currency, I always end up with lots of trailing numbers after the decimal point.
Is there a standard way to do this? Should I truncate or round? Do I need to round multiple times in case rounding the 1/1000 decimal will affect rounding the 1/100 decimal?
Should I be doing something like:
Math.Round(Math.Round(x, 3), 2)
I've been having trouble finding good information about how this is done (hopefully US and Europe are the same).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 Math.Round(x, 2, MidpointRounding.AwayFromZero)
You should use
Math.Round(x, 2, MidpointRounding.AwayFromZero)
我假设你应该能够直接使用 Math.Round 因为 .NET 使用 银行家四舍五入。
只要记住始终将四舍五入绝对作为您所做的最后一件事,以免引入四舍五入错误。
在这样的货币系统中,我还将实际的未舍入值存储为小数,以供将来解决问题之用。
I would assume you should just be able to directly use Math.Round as .NET uses Bankers Rounding.
Just remember to always make rounding be the absolute very last thing you do to not introduce rounding errors.
In a currency system like this, I would also store the real unrounded values as decimals additionally for future problem solving purposes.
如果您的网站有多种货币,则使用它根据当前用户的货币和文化正确舍入小数:
if your site has multiple currencies then use this to correctly round decimals according to the current user's currency and culture :