Number Madness

There is only one number type in JavaScript, the double-precision 64-bit binary format plus 3 symbolic numbers. JavaScript gets a hard time when it comes to working with numbers, but it is not JavaScript that is the problem. The problems occur in our 'human representation' of numbers.
0.1 + 0.7 = 0.8 ( NOPE!!! )

The sticky toffee pudding example

Split a sticky toffee pudding with a friend, and you do 'not' get a half. Cut it with a lazer you still won't get half each. Unless you can ensure that there are an exact number of molocules in each half you do not have half.

Break the sticky toffee pudding into three, and OMG we enter the numeric abyis and the concept of infinity. Imagine pulling a peice off the tray; you have this long sticky trail from your plate to the tray. That peice can never be exactly a third because dividing 1 by 3 is 0.3333333333333333 to infitiity. We have to truncate, if we don't trucate it at some reasonble point. I.e I wouldn't be able to write this blog article as it could never finish writing that last sentence.

The devil is in the detail, nothing more true than with Mathematics.
1 + 7; // 8 ...ok
1.0 + 7.0 // 8 ..still ok
0.10 + 0.70 // OMG my head just exploaded ....why you no 0.8?
The problem as I eluded to above is the representation of division of 1, 10 or 100 or any 1^.
build Dev tools time build Play around with those additions above.

Decimals are representations percents of 10

We know the problem with representations of percents of ten as per the toffee pudding example. So what do we do? We have to truncate into a differnet number. 0.333333333 needs to be 0, 0.3 or 0.33 for example.

Math.floor(0.10 + 0.70);

I would recomend using the Math staic methods rather than cutting the decimal points off and coearcing to a string and back again. That way you have more control. This gives you pricing options depending on your viewpoint.

Math.floor(0.10 + 0.70); // CLO
Math.ceil(0.10 + 0.70);  // CEO
Math.round(0.10 + 0.70); // CTO
Always use non decimal values when dealing with money.
10 + 70 // cents for everyone
console.log("Price is " + ((10 + 70) / 100).toFixed(2) + " USD");
build Dev tools time build Try some of the Number.prototype methods and the staic Math methods.

Safe integers

Wait what's an Integer? If there is only one number type in JavaScript. Then why do we talk about integers?

Browser manufactores are keen to optimize the use of numbers. No point storing 69.0000000000000000000 when you can use 69 internally, and apply double point math when it matters. So they need to be sure that when doing this optimization that they can do this accuratly. It works in many cases but in some cases it falls apart.

var large_number_int = 9007199254740991;
large_number_int + 1; //9007199254740992 ok so far
large_number_int + 2; //9007199254740992 wait what now?

Ok so how do we know if we are safe to use an number or not. We have a Staic Number method called isSafeInteger();

Number.isSafeInteger(9007199254740991); //true
Number.isSafeInteger(9007199254740992); //false

Before working with any integer ensure it is safe. AA quick check would be the Static Number Constant MAX_SAFE_INTEGER which gives you 9007199254740991

*FINAL TIP: When working with currency (and number in general) use integer representaion, and trucnate to a formatteed string only at the last minute.
//better approach
Number((0.10 + 0.70).toFixed(2));

My comments engine is coming...one day. For now, drop me an email if you have stuff to get off your chest