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
const _ = require("lodash");
let gfg = _.round(7.56);
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
const _ = require("lodash");
let gfg = _.round(9.005, 2);
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
const _ = require("lodash");
let gfg = _.round(1980, -2);
console.log(gfg);
|
Output:
2000
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
09 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...