Skip to content Skip to sidebar Skip to footer

Division Of A Float/integer By A Integer In Jquery

I am dividing a number( may be in amount format of XX.XX) by a integer. 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

Solution 1:

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

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.

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.

Solution 2:

You can use .toFixed()

var res = (vall / tot).toFixed(2);
//input `6.6/6`: , output : `1.10`

Demo ---->http://jsbin.com/amileg/4/edit

Post a Comment for "Division Of A Float/integer By A Integer In Jquery"