locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
18 lines (17 loc) • 578 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.round = round;
function round(x, digits) {
// discuss at: https://locutus.io/r/round/
// parity verified: R 4.4
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: round(3.14159)
// returns 1: 3
// example 2: round(3.14159, 2)
// returns 2: 3.14
if (digits === undefined || digits === 0) {
return Math.round(x);
}
const factor = Math.pow(10, digits);
return Math.round(x * factor) / factor;
}