Tag Archives: Round

Round float numbers in Javascript

Why it’s complicated

JavaScript’s Math object provides a method for rounding to whole numbers. If we want to round to a set number of decimal places, then we have to handle that ourselves. This post doesn’t get into the details of floating-point arithmetic, but the short of it is most programming languages use a binary floating-point representation which can only approximate many decimal fractions. This results in rounding errors for the most common approaches to rounding in JavaScript.

Continue reading Round float numbers in Javascript