10 examples of 'js round to 2 decimals' in JavaScript

Every line of 'js round to 2 decimals' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
277function round_decimals(original_number, decimals) {
278 var result1 = original_number * Math.pow(10, decimals)
279 var result2 = Math.round(result1)
280 var result3 = result2 / Math.pow(10, decimals)
281 return pad_with_zeros(result3, decimals)
282}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

59function round(value, decimals) {
60 return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
61}
1export function round(value: any, decimals: any) {
2 if (!decimals) decimals = 0;
3 return Number(Math.round(Number(value + 'e' + decimals)) + 'e-' + decimals);
4}
157static round(value, decimals) {
158 return Number(`${Math.round(`${value}e${decimals}`)}e-${decimals}`)
159}
11function roundOne(decimalNumber) {
12 return roundTo(decimalNumber, 1);
13}
1694function precisionRound(number, precision) {
1695 var factor = Math.pow(10, precision);
1696 return Math.round(number * factor) / factor;
1697}
36function roundNumber(num, decimals) {
37 num = num || 0;
38 if (typeof num === 'number') {
39 return parseFloat(num.toFixed(decimals));
40 } else {
41 return roundArray(num, decimals);
42 }
43}
118export function round1(num: number) {
119 if (+num.toPrecision(4) === (num | 0)) return num;
120 else return num.toPrecision(4);
121}
186function round(val, precision, method) {
187 var fn = math[method || 'round'];
188 var multiplier = math.pow(10, math.abs(precision || 0));
189 if(precision < 0) multiplier = 1 / multiplier;
190 return fn(val * multiplier) / multiplier;
191}
311function convertDecimal(value, surroundDecimalsWith) {
312 if (/^-?([0]|([1-9][0-9]*))(\.[0-9]+)?$/.test(value) == false) {
313 throw new Error("value supposed to be a decimal number but got: " + value);
314 }
315 if (surroundDecimalsWith) {
316 return {
317 value: value,
318 toJSON: function () {
319 return surroundDecimalsWith.str + value + surroundDecimalsWith.str;
320 }
321 };
322 }
323 else {
324 return value;
325 }
326}

Related snippets