10 examples of 'round to 2 decimal places javascript' in JavaScript

Every line of 'round to 2 decimal places javascript' 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
11function roundOne(decimalNumber) {
12 return roundTo(decimalNumber, 1);
13}
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

17function roundTo(value, places) {
18 var mult = Math.pow(10, places);
19 return Math.round(value * mult) / mult;
20}
11function roundNumber(number,decimalPlaces) //Used to round a number to a certain number of decimal places.
12{
13var placeSetter = Math.pow(10, decimalPlaces);
14number = Math.round(number * placeSetter) / placeSetter;
15return number;
16}
157static round(value, decimals) {
158 return Number(`${Math.round(`${value}e${decimals}`)}e-${decimals}`)
159}
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}
125static roundDecimal(number, precision) {
126 let factor = Math.pow(10, precision);
127 return Math.round(number * factor) / factor;
128}
1694function precisionRound(number, precision) {
1695 var factor = Math.pow(10, precision);
1696 return Math.round(number * factor) / factor;
1697}
61export function roundDown(
62 value: BigNumber.Value,
63 decimals: number = 2,
64 roundingTolerance: number = 0
65): BigNumber {
66 if (roundingTolerance) {
67 value = new BigNumber(value).decimalPlaces(
68 decimals + roundingTolerance,
69 BigNumber.ROUND_HALF_DOWN
70 )
71 }
72 return new BigNumber(value).decimalPlaces(decimals, BigNumber.ROUND_DOWN)
73}
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