Use the . toFixed() Method to Round a Number To2 Decimal Places in JavaScript. We apply the . toFixed() method on the number and pass the number of digits after the decimal as the argument.
Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places. Copied!
The Math. round() function in JavaScript is used to round the number passed as parameter to its nearest integer. Parameters : The number to be rounded to its nearest integer.
To round a number to the nearest 5, call the Math. round() function, passing it the number divided by 5 and multiply the result by 5 .
/**
* @param num The number to round
* @param precision The number of decimal places to preserve
*/
function roundUp(num, precision) {
precision = Math.pow(10, precision)
return Math.ceil(num * precision) / precision
}
roundUp(192.168, 1) //=> 192.2
Little late but, can create a reusable javascript function for this purpose:
// Arguments: number to round, number of decimal places
function roundNumber(rnum, rlength) {
var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
return newnumber;
}
Call the function as
alert(roundNumber(192.168,2));
Normal rounding will work with a small tweak:
Math.round(price * 10)/10
and if you want to keep a currency format, you can use the Number method .toFixed()
(Math.round(price * 10)/10).toFixed(2)
Though this will make it a String =)
Very near to TheEye answer, but I change a little thing to make it work:
var num = 192.16;
console.log( Math.ceil(num * 10) / 10 );
The OP expects two things:
A. to round up to the higher tenths, and
B. to show a zero in the hundredths place (a typical need with currency).
Meeting both requirement would seem to necessitate a separate method for each of the above. Here's an approach that builds on suryakiran's suggested answer:
//Arguments: number to round, number of decimal places.
function roundPrice(rnum, rlength) {
var newnumber = Math.ceil(rnum * Math.pow(10, rlength-1)) / Math.pow(10, rlength-1);
var toTenths = newnumber.toFixed(rlength);
return toTenths;
}
alert(roundPrice(678.91011,2)); // returns 679.00
alert(roundPrice(876.54321,2)); // returns 876.60
Important note: this solution produces a very different result with negative and exponential numbers.
For the sake of comparison between this answer and two that are very similar, see the following 2 approaches. The first simply rounds to the nearest hundredth per usual, and the second simply rounds up to the nearest hundredth (larger).
function roundNumber(rnum, rlength) {
var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
return newnumber;
}
alert(roundNumber(678.91011,2)); // returns 678.91
function ceilNumber(rnum, rlength) {
var newnumber = Math.ceil(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
return newnumber;
}
alert(ceilNumber(678.91011,2)); // returns 678.92
ok, this has been answered, but I thought you might like to see my answer that calls the math.pow()
function once. I guess I like keeping things DRY.
function roundIt(num, precision) {
var rounder = Math.pow(10, precision);
return (Math.round(num * rounder) / rounder).toFixed(precision)
};
It kind of puts it all together. Replace Math.round()
with Math.ceil()
to round-up instead of rounding-off, which is what the OP wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With