Skip to content
Advertisement

Is their any way I can **not** round off the number while using parseInt?

let display = '5+10.10';
let numbers = display.match(/(d+.?d*|.d+)/g).map(a => parseInt(a));
console.log(numbers)//returns [5, 10]

when I don’t use parseInt:

let display = '5+10.10';
let numbers = display.match(/(d+.?d*|.d+)/g);
console.log(numbers)//returns ["5", "10.10"]

I need the array item’s as an number with decimal rather than a string.

Advertisement

Answer

parseInt converts to Integer. You want parseFloat :

let display = '5+10.10';
let numbers = display.match(/(d+.?d*|.d+)/g).map(a => parseFloat(a));
console.log(numbers)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement