jQuery中的浮点数/整数除以整数 [英] Division of a float/integer by a integer in jquery

查看:170
本文介绍了jQuery中的浮点数/整数除以整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数字(可能是XX.XX的金额格式)除以整数.

I am dividing a number( may be in amount format of XX.XX) by a integer.

但是当我执行6.6/6或3.3/3时,它将给出1.099999 ...而不是1.1.

But when i do 6.6 / 6 or 3.3/3 , it gives 1.099999... , instead of 1.1.

我正在做的是,我将金额平均分配给人数,如果有剩余金额,我会将额外的金额加到其中的一个人上.

What i am doing is, I am dividing the amount equally among the number of people and if any remainder amount exists, I am adding that extra amount to one person among them.

可能是这种情况,将104/6分为17.85、17.83、17.83、17.83、17.83、17.83 [可能不准确,仅用于说明].

May be something of this kind, 104/6 will be divided as 17.85, 17.83, 17.83, 17.83, 17.83, 17.83 [ May not be accurate, just for illustration].

JS Bin链接----> http://jsbin.com/amileg/1/edit

JS Bin link ----> http://jsbin.com/amileg/1/edit

请帮助我获得正确的计算逻辑.

Kindly help me in getting the right calculation logic.

谢谢!

推荐答案

这是浮点运算,并且是舍入错误.您可以阅读以了解更多信息.

That is floating point arithmetic and those are rounding errors . You can read this for more.

将无限多个实数压缩为有限数量的位需要近似表示.尽管有无限多个整数,但是在大多数程序中,整数计算的结果可以存储在32位中.相反,在给定固定位数的情况下,大多数使用实数的计算将产生无法使用那么多位数精确表示的数量.因此,浮点计算的结果通常必须四舍五入,以重新适合其有限表示形式.舍入误差是浮点计算的特征.

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

JavaScript中的所有数字均以 IEEE-754 Doubles 二进制表示.精度约为14或15个有效数字.它们并不总是精确地表示实数,包括分数.

All numbers in JavaScript are represented in binary as IEEE-754 Doubles, which provides an accuracy to about 14 or 15 significant digits. They do not always exactly represent real numbers, including fractions.

这篇关于jQuery中的浮点数/整数除以整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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