I need help with replaceAll for monetary values!

Asked

Viewed 33 times

0

String sal = "1500,32";
double salario = 0;
sal.replaceAll( ",", ".");
salario = Double.parseDouble(sal);

Can anyone explain to me better the replaceAll? is giving error!

1 answer

3

The replaceAll returns a String and does not change the current one. You need to do the following for your code to work:

String sal = "1500,32";
double salario = 0;
sal = sal.replaceAll(",", ".");
salario = Double.parseDouble(sal);

But bear in mind that there are better ways to convert monetary values.

  • Thank you very much, man!

  • @Joelmedeiros don’t forget to accept the answer so you can help other people with similar doubt.

Browser other questions tagged

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