How can I round up different type of currencies with using jquery? What I'm trying to do is;
10.30 → 10 //Below .5 will round down
10.60 → 11 //After .5 will round up
849.95 → 850
1,022.20 → 1022
How can I round up different type of currencies with using jquery? What I'm trying to do is;
10.30 → 10 //Below .5 will round down
10.60 → 11 //After .5 will round up
849.95 → 850
1,022.20 → 1022
You can use Math.round()
The
Math.round()
function returns the value of a number rounded to the nearest integer.
Please Note: The number with comma is a string and you have to remove the comma. Then you can convert that back to number with Number()
, finally implement Math.round()
console.log(Math.round(10.30));// → 10 //Below .5 will round down
console.log(Math.round(10.60))// → 11 //After .5 will round up
console.log(Math.round(849.95));// → 850
console.log(Math.round(Number('1,022.20'.replace(/,/g, ''))));// → 1022
Or you can use a jquery plugin: https://github.com/autoNumeric/autoNumeric Then it's as simple as:
new AutoNumeric('.myInput', {
roundingMethod: AutoNumeric.options.roundingMethod.halfUpSymmetric
});