为什么模数运算符在javascript中返回小数? [英] Why does modulus operator return fractional number in javascript?

查看:119
本文介绍了为什么模数运算符在javascript中返回小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JavaScript中的 49.90%0.10 返回 0.09999999999999581 ?因为JavaScript使用浮点数学,总是会导致舍入误差。$ / $>

解决方案

b
$ b

如果您需要两位小数的精确结果,请在操作前将您的数字与 100 相乘,然后再进行除法: p>

  var result =(4990%10)/ 100; 

如果需要,循环。


Why does 49.90 % 0.10 in JavaScript return 0.09999999999999581? I expected it to be 0.

解决方案

Because JavaScript uses floating point math which always leads to rounding errors.

If you need an exact result with two decimal places, multiply your numbers with 100 before the operation and then divide again afterwards:

var result = ( 4990 % 10 ) / 100;

Round if necessary.

这篇关于为什么模数运算符在javascript中返回小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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