How to extract the numerical part of a monetary value?

Asked

Viewed 486 times

5

I have a string with the characters "R$ 1,000.60" and would like to extract only the characters "1,000.60".

I tried using regular expression, but I couldn’t reach my goal. I got to this:

    string valor = "";
    string texto = "R$ 1.000,60";

    Regex r = new Regex(@"^[0-9]*(?:\.[0-9]*)?$");
    foreach (Match m in r.Matches(texto))
        valor += m.Value;

Would anyone know how to solve this problem using regular expression or other technique?

  • +1 including by the goodwill of Edit :) We already have a good solution with Regex, and more alternatives may arise for you, the same Gypsy and/or other users.

  • 1

    A split wouldn’t be enough?

  • 1

    It is guaranteed that the format is always starting with R$ and then comes the number that matters in the format shown?

  • The values will not always start with R$, and you can also change the field orders, for example 50R$

1 answer

3


  • 1

    Why the use of var and not Regex?

  • 1

    The compiler is able to intuit the type of the variable to be returned.

Browser other questions tagged

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