0

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
m12l
  • 65
  • 9
  • Please search before asking new questions. Rounding is a simple concept that has many very similar questions already. – str Dec 15 '19 at 14:11

2 Answers2

3

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
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • Me again, tried your version to fix it but I'm having a problem if the currency from Europe. For example €22,34. But it works perfectly fine with rest of it. https://jsfiddle.net/ug2veqbh/ – m12l Dec 15 '19 at 18:25
0

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 
});

geoidesic
  • 4,649
  • 3
  • 39
  • 59