smachs
smachs

Reputation: 236

Formatting values with javascript

in a previous question was having doubts with the removal of all the points and commas of my array but for accounting of these values this is bad how can I replace the commas of my cents in points?

var arr = ['1,00',
'10,00',
'500,00',
'5.000,00',
'70.000,00',
'900.000,00',
'1.000.000,00']

arr.map(value => alert(Number(value.split(',')[0].replace(/[^0-9]+/g,""))))

https://jsfiddle.net/smachs/fdchzupm

Example of the result that I hope:

1,98 => 1.98
10,50 => 10.53
500,47 => 500.47
5.000,64 => 5000.64
70.000,29 => 70000.29
900.000,16 => 900000.16
1.000.000,07 => 1000000.07

Thanks for help me xD

Upvotes: 1

Views: 37

Answers (1)

Titus
Titus

Reputation: 22474

This should do it:

var arr = ['1,00',
'10,00',
'500,00',
'5.000,00',
'70.000,00',
'900.000,00',
'1.000.000,00']

arr = arr.map(v => v.replace(/\./g, "").replace(",", "."));
console.log(arr);

The important part is .replace(/\./g, "").replace(",", ".") what this does is to first remove all . characters from the string and then replace the , character with ..

Upvotes: 2

Related Questions