Lodash _.round() Method
Lodash _.round() method is used to compute numbers rounded to precision.
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
Syntax:
_.round(number, [precision = 0])
Parameters:
This method accepts two parameters as mentioned above and described below:
- number: This parameter holds the number to round.
- [precision = 0]: This parameter holds the precision to round to.
Return Value:
This method returns the rounded number.
Example 1: In this example, the lodash library’s _.round()
method is used to round the number 7.56 to the nearest integer, resulting in the output value of 8.
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.round() // method let gfg = _.round(7.56); // Printing the output console.log(gfg); |
Output:
8
Example 2: In this example, the lodash library’s _.round()
method is applied to round the number 9.005 to 2 decimal places, resulting in the output value of 9.01.
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.round() // method let gfg = _.round(9.005, 2); // Printing the output console.log(gfg); |
Output:
9.01
Example 3: In this example, the lodash library’s _.round()
method is utilized to round the number 1980 to the nearest hundred (with -2 decimal places), resulting in the output value of 2000.
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.round() // method let gfg = _.round(1980, -2); // Printing the output console.log(gfg); |
Output:
2000