How to make a number from string with comma separators in JavaScript ("23,21" ---> 23,21)

Viewed 69

Does anyone know how to make a number from a string with comma separators, in JS. I got: "23,21" and I want to get it as a number value.

"23,21" --> 23,21

3 Answers

You can but not with the "comma" (,). In some countries people do use commas instead of dots to write decimals. If that's the case then what you can do is:

let a = "23,25";
let b = a.replaceAll(',', '.');
console.log(parseFloat(b));

But doing the this with the comma is also not wrong. But you will only get the part before the decimal.

i.e. 23 in your case

Check if you want this.

var str = '23,21';
console.log((str.split(",").map((i) => Number(i))));

UPDATE

 var str = '23,21';
 var arr = str.split(",").map((i) => Number(i));
 console.log(parseFloat(arr.join('.')));
Related