Using Math.floor() is one way of doing this.
More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
Answer from phoebus on Stack OverflowUsing Math.floor() is one way of doing this.
More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
Round towards negative infinity - Math.floor()
+3.5 => +3.0
-3.5 => -4.0
Round towards zero can be done using Math.trunc(). Older browsers do not support this function. If you need to support these, you can use Math.ceil() for negative numbers and Math.floor() for positive numbers.
+3.5 => +3.0 using Math.floor()
-3.5 => -3.0 using Math.ceil()
Videos
Use Math.round() :
Math.round(num * 100) / 100
Or to be more specific and to ensure things like 1.005 round correctly, use Number.EPSILON :
Math.round((num + Number.EPSILON) * 100) / 100
If the value is a text type:
parseFloat("123.456").toFixed(2);
If the value is a number:
var numb = 123.23454;
numb = numb.toFixed(2);
There is a downside that values like 1.5 will give "1.50" as the output. A fix suggested by @minitech:
var numb = 1.5;
numb = +numb.toFixed(2);
// Note the plus sign that drops any "extra" zeroes at the end.
// It changes the result (which is a string) into a number again (think "0 + foo"),
// which means that it uses only as many digits as necessary.
It seems like Math.round is a better solution. But it is not! In some cases it will not round correctly:
Math.round(1.005 * 100)/100 // Returns 1 instead of expected 1.01!
toFixed() will also not round correctly in some cases (tested in Chrome v.55.0.2883.87)!
Examples:
parseFloat("1.555").toFixed(2); // Returns 1.55 instead of 1.56.
parseFloat("1.5550").toFixed(2); // Returns 1.55 instead of 1.56.
// However, it will return correct result if you round 1.5551.
parseFloat("1.5551").toFixed(2); // Returns 1.56 as expected.
1.3555.toFixed(3) // Returns 1.355 instead of expected 1.356.
// However, it will return correct result if you round 1.35551.
1.35551.toFixed(2); // Returns 1.36 as expected.
I guess, this is because 1.555 is actually something like float 1.55499994 behind the scenes.
Solution 1 is to use a script with required rounding algorithm, for example:
function roundNumber(num, scale) {
if(!("" + num).includes("e")) {
return +(Math.round(num + "e+" + scale) + "e-" + scale);
} else {
var arr = ("" + num).split("e");
var sig = ""
if(+arr[1] + scale > 0) {
sig = "+";
}
return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
}
}
It is also at Plunker.
Note: This is not a universal solution for everyone. There are several different rounding algorithms. Your implementation can be different, and it depends on your requirements. See also Rounding.
Solution 2 is to avoid front end calculations and pull rounded values from the backend server.
Another possible solution, which is not a bulletproof either.
Math.round((num + Number.EPSILON) * 100) / 100
In some cases, when you round a number like 1.3549999999999998, it will return an incorrect result. It should be 1.35, but the result is 1.36.
Use Math.floor to round the decimal places under the current value.
Reference
Example
Math.floor(1.0789 * 100) / 100
Working Fiddle
console.log(Math.floor(1.0789 * 100) / 100);
console.log(Math.floor(10.350 * 100) / 100);
console.log(Math.floor(1.7777 * 100) / 100);
console.log(Math.floor(12.34 * 100) / 100);
you have several methods for do this
- Use Math.round(num * 100) / 100
- Use Math.ceil(num * 100)/100;