round.js
Round to the nearest whole number.
Works in all modern browsers, and at least back to IE9.
/*!
* Round to the nearest whole number
* (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Number|String} num The numer to round
* @param {Number} precision The whole number to round to (ex. 10, 100, 1000)
* @param {String} method The rounding method (up, down, or auto - defaults to auto) [optional]
* @return {String} The rounded, delimited number
*/
var round = function (num, precision, method) {
// Convert string numbers to a float
num = parseFloat(num);
// If there's no rounding precision, return the number
if (!precision) return num.toLocaleString();
// Possible methods and their values
var methods = {
auto: 'round',
up: 'ceil',
down: 'floor'
};
// Get the method function
var fn = methods[method];
if (!fn) {
fn = 'round';
}
// Do math!
return (Math[fn](num / precision) * precision).toLocaleString();
};