This question already has an answer here:
- Is floating point math broken? 31 answers
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 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.
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 link ----> http://jsbin.com/amileg/1/edit
Kindly help me in getting the right calculation logic.
Thanks!!
That is floating point arithmetic and those are rounding errors . You can read this for more.
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.
You can use
.toFixed()
Demo
---->
http://jsbin.com/amileg/4/edit