乘以100.0介绍舍入误差的双重价值? [英] Multiplying a double value by 100.0 introduces rounding errors?

查看:153
本文介绍了乘以100.0介绍舍入误差的双重价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在做什么,它的工作原理99.999%的时间:

This is what I am doing, which works 99.999% of the time:

((int)(customerBatch.Amount * 100.0)).ToString()

的数量值是一个双。我想写的价值在美分至传输到服务器进行处理的文本文件。量从未超过2 precision数字。

The Amount value is a double. I am trying to write the value out in pennies to a text file for transport to a server for processing. The Amount is never more than 2 digits of precision.

如果您使用580.55的金额,此行code返回58054为字符串值。

If you use 580.55 for the Amount, this line of code returns 58054 as the string value.

这code运行在64位Web服务器上。

This code runs on a web server in 64-bit.

任何想法?

推荐答案

您真的应该使用十进制货币计算。

You should really use decimal for money calculations.

((int)(580.55m * 100.0m)).ToString().Dump();

这篇关于乘以100.0介绍舍入误差的双重价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆