Integer vs Float

Precision and floating point arithmetic.

Examples

Floating Point Precision

Demonstrating floating point inaccuracy.

let x = 0.2 + 0.1;
console.log(x); // 0.30000000000000004

Solving Precision Issues

Multiplying and dividing to ensure integer arithmetic.

let x = (0.2 * 10 + 0.1 * 10) / 10;
console.log(x); // 0.3

Integer Accuracy

Max safe integer precision is 15 digits.

let x = 999999999999999;
let y = 9999999999999999;
console.log(x); // Correct
console.log(y); // Rounds up to 10000...

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.