Currency Value to Double Conversion

Asked

Viewed 421 times

2

I have the following value in Double: 1000.0

Using the command: string.Format(CultureInfo.GetCultureInfo("pt-BR"), "{0:C}", 1000.0);

I have the exit: R$ 1,000,00

My question is, how to convert R $ 1,000.00 to double, ie get the output 1000.0

  • 2

    And why would you do this conversion? Check it out: http://answall.com/a/38140/101

  • 1

    Could explain better why you convert the value into double to string type and then need to convert it back to double?

  • Imagine a sales screen that exists a Textbox called "Discount". This "Discount" field has values like R$ 1,000.00, where the user type manually. After this typing, I do the calculation Qtd x Unit Value - Discount to get the Total value, so I want to convert the currency to Double.

1 answer

-1

If vc is using for display vc can put as in the example below:

<DataGridTextColumn Header="Valor Fator" Binding="{Binding PendenciaValorFator, Mode=TwoWay, StringFormat=N, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">

I believe you are stringFormat and assigning a property to show on the screen, when what you have to do is use stringFormat in the view, so your property with 1000.0 will not be changed.

  • So, my property already starts with the values "R$ 1,000,00" because it is a value typed by the user.

  • Does the user type "R$"? If yes, one of the options is to change the way the system is used to type only the value, or if it is not possible you can do Convert.Todecimal(text.Replace("R$ ", "")), algo do tipo... Remembering that this will be susceptible to a wide range of possible errors.

  • I also found in SO-En this solution: http://stackoverflow.com/questions/14162357/convert-currency-string-to-decimal

  • You can use: decimal. Parse(input, Numberstyles.Allowcurrencysymbol | Numberstyles.Number); ou: decimal.Parse(Regex.Match(input, @"-? d{1,3}(, d{3})*(. d+)?" ). Value);

  • I understand, I will treat so that the user does not type "R$", but just numbers and comma, so he would type 1000.00 and I use the Convert.toDouble method.

  • Just place a label next to the text field with the "R$" content to indicate that it is monetary value. Then don’t forget to make the appropriate validations for the user not to enter text in the value. I would appreciate it if you would mark as an answer. Thank you.

  • @Lucasduzo managed to solve his problem?

Show 2 more comments

Browser other questions tagged

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