1

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

6
  • 1
    Why would you use alert in the map function? Commented May 6, 2018 at 14:16
  • @Xufox are only test I will revert this logic to json formatting Commented May 6, 2018 at 14:18
  • Hint: use console.log() instead of alert() Commented May 6, 2018 at 14:20
  • @Xufox yes but jsfiddle only displays with alert or document.write Commented May 6, 2018 at 14:46
  • @smachs Have you opened the developer tools to see the console logs? Commented May 6, 2018 at 15:36

1 Answer 1

2

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 ..

Sign up to request clarification or add additional context in comments.

1 Comment

Also should point out needs to be string not Number to display .00 as decimal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.