简体   繁体   中英

How to round of prices with comma's instead of dots?

In The Netherlands we use comma's in numbers where in other countries dots would be used. For example we use 39,99 and in other countries 39.99.

In a feed with prices we would have prices with such comma use, but I'm having trouble using those as numbers and rounding them by two digits behind the comma (or behind the dot really).

var num1 = "39,1234";
var num = parseInt(num1);
var n = num.toFixed(2);
console.log(n);

Here is such a number. I would like it to result in 39,12. They way I was thinking is then first use it as a string. Then turn that string into a number and use toFixed to round it of to two digets. But it results in 39,00 instead of 39,12.

Perhaps I'm thinking wrong and I should use some other way to make 39,1234 to be seen as 39.1234 so that it is rounded correctly as a number?

How can I used 39,1234 as a number 39,1234 instead of a string? So that I wouldn't have to go through a feed and replace commas by dots first in all my prices?

Edit: Regex version

Earlier I didn't realize that OP originally wanted it back to the format "xx,xx". This is a more elegant solution:

 var num1 = "39,1234"; let n = num1.replace(/(?<=,\\d{2})(\\d*)$/,""); console.log(n); //32,12 

Regex explanation:

(?<=,\\d){2} begins a lookbehind match for , followed by digits \\d , 2 of them {2} . Lookbehind matches are not replaced.

(\\d*)$ when we've found the lookbehind pattern, we match more digits \\d , all * of them, till we reach end of string $ . This is the match that will get replaced.

Original Solution

What you want is:

 var num1 = "39,1234"; var n = parseFloat(num1.replace(",",".")).toFixed(2); console.log(n); //39.12 // replaces it back to ",", but now it's a string! n = n.replace(".",",") console.log(n); //39,12 

Explanation:

  1. First replace "," with "." with replace()

  2. Convert to float (not integer) with parseFloat()

  3. Set to 2 decimal places with .toFixed(2)

  4. Replace "." with ",". But now it's a string!

Note: this will not work if the currency value contains . as a thousandth separator. eg "40.200,1576". If that's the case, add another line num1 = num1.replace(".","") to strip out the separator before passing it to the parseFloat(...) line.

Try this

comdecimal= num1.replace(".","")
alert(comdecimal);
dotdecimal= comdecimal.replace(",",".") 
alert(dotdecimal); 
dotdecimal = Math.round(dotdecimal* 100) / 100;
alert(dotdecimal);

Since you're working with currency, I'd recommend using JS ES6 designated NumberFormat feature. Your code should look like this and be easily reused:

 const formatter = new Intl.NumberFormat('nl-NL', { style: 'currency', currency: 'EUR', minimumFractionDigits: 2 }); console.log(formatter.format('145,53'.replace(',','.'))); //"€ 145,53" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM