parseFloat rounding

前端 未结 5 1210
太阳男子
太阳男子 2020-12-01 12:06

I have javascript function that automatically adds input fields together, but adding numbers like 1.35 + 1.35 + 1.35 gives me an output of 4.050000000000001, just as an exam

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 12:39

    var num = 4.050000000000001;
    
    num = num.toFixed(2);
    

    toFixed will round up depending on how many digits after the decimal you're looking for.

提交回复
热议问题