Convert Real values to USD

Asked

Viewed 90 times

-3

I have some string values in the following format?

1.599,90

I need them to become a dollar value, like the below:

1599.90

I tried to convert to float, but the value obtained is not correct:

console.log(parseFloat("1.599,90"))

I’ve researched enough and convert values into real to dollar I couldn’t find except the opposite.

  • Lately it is difficult to ask for help here, since in addition to not helping, they still disqualify the doubt of people.

1 answer

2


If the result can be obtained in string, you can build a function like this:

function realToDolar(num){
    num = num.replace('.', '');
    num = num.replace(',', '.');
    return num
}

It formats the input value by removing the thousand point and replacing the decimal point to a point. Just run the function:

var numeroReal = "1.599,90";
var numeroDolar = realToDolar(numeroReal); // retorna "1599.90"
  • 1

    You need to use a regular expression, or else the method replace will only replace the first instance found in the string. If the number is '1.000.599,90', this solution will not work.

  • How this would be done with regular expression?

  • 2

    @Otavio, instead of num.replace('.', ''), would be num.replace(/\./g, '')

  • Thank you very much guys. Although some people have dismissed my question, I appreciate you two taking the time to help me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.