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
CodePudding user response:
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
CodePudding user response:
Just like that
parseFloat("23,21".replace(",", "."));
CodePudding user response:
You can use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
But before you need to replace comma for dot.
parseFloat('23,21'.replace(/,/, '.')); // == 23.21
let x = '16,5';
parseFloat(x.replace(/,/, '.')); // == 16.5