add two double given wrong result

前端 未结 5 1115
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 10:33

I\'m using the following piece of code and under some mysterious circumstances the result of the addition is not as it\'s supposed to be:

double _west = 9.482935         


        
5条回答
  •  暖寄归人
    2021-01-22 10:53

    You are being bitten by rounding and precision problems. See this. Decimal might help. Go here for details on conversions and rounding.

    From MSDN:

    When you convert float or double to decimal, the source value is converted to decimal representation and rounded to the nearest number after the 28th decimal place if required. Depending on the value of the source value, one of the following results may occur:

    If the source value is too small to be represented as a decimal, the result becomes zero.

    If the source value is NaN (not a number), infinity, or too large to be represented as a decimal, an OverflowException is thrown.

提交回复
热议问题