Categories:Viewed: 57 - Published at: 6 months ago

Introduction

Floats and doubles represent floating-point numbers - numbers with decimal points. While this type is very useful for a wide variety of environments, depending on what they're used for, we sometimes want to round them to a whole number - closest whole integer, up or down.

In this article, we will explore how to round double/float numbers (including strings) in JavaScript using built-in methods of the Math object.

The Math Object

The Math object is used to conduct commonplace mathematical operations. Because the Math object is a static built-in object, you can't and won't need to initialize it - it can be invoked at any point as a static object with helper methods. It has a plethora of constants and techniques for doing mathematical operations, all of which may be accessed directly. Let's look at some of the Math object's handy methods for rounding doubles/floats in JavaScript!

Math.round()

Math.round() is a function used to return the value of a floating-point number rounded to the nearest whole integer. Depending on the supplied float/double, Math.round() either rounds up or down. If the floating-point number is above or equal to x.5 - it's rounded up to the nearest integer. If the floating-point number is below x.5, it's rounded down to the nearest integer:

Math.round(24.49); // 24
Math.round('29.5'); // 30
Math.round(72); // 72
Math.round(-40.51); // -41
Math.floor()

The Math.floor() method is used to return the nearest whole integer that is less than or equal to a specified value. In layman's terms, the floor() method rounds a number down and returns an integer result:

Math.floor(75.95); //  75
Math.floor(75.05); //  75
Math.floor(4); //   4
Math.floor('-65.05'); // -66
Math.floor(-65.95); // -66
Note: Math.floor(null) returns 0, not a NaN error.
        <h5 id="mathceil"><em>Math.ceil()</em></h5>

The Math.ceil() method rounds a number up to the nearest greatest integer. Simply said, it is used to round up a number and return an integer value:

Math.ceil(0.95);    // 1
Math.ceil(7);      // 7
Math.ceil('9.008');  // 10
Math.ceil(-0.95);  // -0
Math.ceil(-7);     // -7
Math.ceil(-9.008); // -10
Note: Math.ceil(null) returns integer 0 and does not give a NaN error.
        <h5 id="mathtrunc"><em>Math.trunc()</em></h5>

While truncating isn't rounding - it's worth mentioning the Math.trunc() method. It returns the whole integer of the number, by truncating (cutting off) the floating-point precision, regardless of whether the argument is positive or negative:

Math.trunc(38.37);    // 38
Math.trunc(72.84);    // 72
Math.trunc(0.123);    //  0
Math.trunc(-0.13323);   // -0
Math.trunc('-1.18923'); // -1
Math.trunc(NaN);      // NaN
Math.trunc('john');    // NaN
Math.trunc();         // NaN

Conclusion

In this short article, we've taken a look at how to round doubles/floats in JavaScript, with the help of Math.round(), Math.floor(), Math.ceil() and Math.trunc().

Reference: stackabuse.com