Converting foreign currency to decimal value

Locked Converting foreign currency to decimal value

  • mercoledì 25 giugno 2008 19:37
     
     
    I am working on a project that has users entering Russian Currency in textboxes.  I need to convert the entered text to decimal values to perform calculations with the entered values.  I am having problems with getting this done.  The code I developed follows:

    Public Class RU
        Private Nfi As NumberFormatInfo = New CultureInfo("ru-RU", False).NumberFormat

        private sub LoadRU
            Nfi.CurrencyDecimalSeparator = ","
            Nfi.CurrencyDecimalDigits = 2
            Nfi.CurrencySymbol = "p"
            Nfi.CurrencyGroupSizes = New Integer() {3}
            Nfi.CurrencyGroupSeparator = " "
        End Sub

        Private Sub uxPrincipalAmount_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles _                                                                    uxPrincipalAmount.LostFocus
           CalculateTotalDue
        End Sub

        Private Sub CalculateTotalDue()
            uxTotalDue.Text = FormatCurrency(CDec(uxPrincipalAmount.Text)) + CDec(IIf(uxInterest.Text = "", 0, uxInterest.Text)) + _                     CDec(IIf(uxAdditionalFees.Text = "", 0, uxAdditionalFees.Text))
        End Sub

        Error Received is: Приведение строки "3 500,00p" к типу 'Decimal' является недопустимым. translated to english "Reduction of a line " 3 500,00p " to type ' Decimal ' is inadmissible."
    The currency amount entered was "3 500,00p"  This is the Russian format for entering currency as I know it to be correct.

    I have also tried this:
        convert.ToDecimal("3 500,00p",Nfi)
    and received this message: "Input string was not in the correct format."

    If I remove the "p", which is the currency symbol, from the statement as follows:
        convert.ToDecimal("3 500,00",Nfi)
    3500D
    Which is what I would expect to be the return.

    Am I not seeing something here with the NumberFormat?  Should the convert work with the currency symbol in the string?  I can remove the currency symbol if that is the "fix" for this.  Thanks in advance.




Tutte le risposte

  • mercoledì 25 giugno 2008 19:58
    Moderatore
     
     Con risposta
    The problem is that the CDec() function does not take the format info as a parameter.  Change all your CDec() to:
     
    Decimal.Parse(stringvalue, Globalization.NumberStyles.Currency, Nfi)

    And then it should work as expected.
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
  • giovedì 26 giugno 2008 11:52
     
     
    Thank you for the reply, that worked perfectly.  Have a great day.
  • mercoledì 18 luglio 2012 22:14
     
     
    THANK YOU Reed!  I've been struggling with this and your answer is the only one that worked!