Multiplying a double value by 100.0 introduces rounding errors?

前端 未结 8 1055
后悔当初
后悔当初 2020-12-19 07:22

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

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

The Amount value is a double. I

8条回答
  •  眼角桃花
    2020-12-19 07:55

    You really should not be using a double value to represent currency, due to rounding errors such as this.

    Instead you might consider using integral values to represent monetary amounts, so that they are represented exactly. To represent decimals you can use a similar trick of storing 580.55 as the value 58055.

提交回复
热议问题