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)