-1
Hello, I am studying regular expressions and trying to make a function to format values. The idea is to receive a value and return it formatted. Example:
- 400.00 --> 400,00
- 150000.00 --> 150.000,00
- 1500.00 --> 1.500,00
The solution I arrived at was this:
priceFormat(value){
let price = value.toString().split('').reverse().join('').replace('.','')
price = price.replace(/(\d{2})/, '$1,')
price = price.replace(/(\d{3}(?!$))/g, '$1.')
return price.split('').reverse().join('')
}
I decided to flip the string to format it backwards, it seemed easier. I believe there must be a more interesting way to do this task, using a regex perhaps, some idea?
The parameter
value
will just get the guyNumber
or the type may also be passedString
? I ask why I want to know if what is passed in the parameter can or cannot be considered that the parameter will always be converted intoNumber
.– Augusto Vasques
It may be String, so much so that the first thing I did was convert because it comes as Number
– Alisson Silva
It could not only replace (dot) '.' by ',' (comma)?
– Danizavtz