Javascript float subtract

前端 未结 5 715
面向向阳花
面向向阳花 2020-12-01 16:20

I was wondering how can I subtract two negative Floating-Point numbers in javascript. I tried:

alert(-0.2-0.1);

and the result is -0.

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 17:01

    No, nothing wrong with your code, most decimal fractions cannot be represented exactly in binary, use

    number.toFixed(x)
    

    Where x is the number of decimals you want and number is the result of the subtraction.

提交回复
热议问题