String Conversion Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TC

    String Conversion Question

    Hey All,

    I have strings in currency. For example: "$1.29"

    What is the best way to convert them to decimal formatted like: "1.29"

    Thanks,

    TC


  • rowe_newsgroups

    #2
    Re: String Conversion Question

    On Aug 3, 8:58 pm, "TC" <getmyemai...@y ahoo.comwrote:
    Hey All,
    >
    I have strings in currency.  For example:  "$1.29"
    >
    What is the best way to convert them to decimal formatted like:  "1.29"
    >
    Thanks,
    >
    TC
    Lookup Decimal.Parse and Decimal.ParseEx act

    Thanks,

    Seth Rowe [MVP]

    Comment

    • TC

      #3
      Re: String Conversion Question

      Decimal.Parse was also giving me a number with 4 decimals (i.e. adding 2
      zeroes).


      "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
      news:9463d36f-6e53-4f76-8197-e05901d79f9d@w7 g2000hsa.google groups.com...
      On Aug 3, 8:58 pm, "TC" <getmyemai...@y ahoo.comwrote:
      Hey All,
      >
      I have strings in currency. For example: "$1.29"
      >
      What is the best way to convert them to decimal formatted like: "1.29"
      >
      Thanks,
      >
      TC
      Lookup Decimal.Parse and Decimal.ParseEx act

      Thanks,

      Seth Rowe [MVP]



      Comment

      • TC

        #4
        Re: String Conversion Question

        I tried Decimal.Parse and it's throwing me an error stating that the string
        is not in the correct format.



        "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
        news:9463d36f-6e53-4f76-8197-e05901d79f9d@w7 g2000hsa.google groups.com...
        On Aug 3, 8:58 pm, "TC" <getmyemai...@y ahoo.comwrote:
        Hey All,
        >
        I have strings in currency. For example: "$1.29"
        >
        What is the best way to convert them to decimal formatted like: "1.29"
        >
        Thanks,
        >
        TC
        Lookup Decimal.Parse and Decimal.ParseEx act

        Thanks,

        Seth Rowe [MVP]



        Comment

        • Martin H.

          #5
          Re: String Conversion Question

          Hello TC,

          You could do this:

          Private Sub Test()
          Dim CurString As String = "$1.29"
          Dim CurString2 As String = "EUR 3.33"

          Dim CurDecimal As Decimal = FormatCurrency( CurString)
          Dim CurDecimal2 As Decimal = FormatCurrency( CurString2)
          End Sub

          Private Function FormatCurrency( ByVal CurString As String) As Decimal
          Dim tmp As String = Replace(CurStri ng, "$", "")
          Dim retVal As Decimal

          'Uncomment the following statement if you use "," for decimal.
          'tmp = Replace(tmp, ".", ",")


          Try
          retVal = CDec(tmp)
          Catch
          retVal = FormatCurrency2 (tmp)
          End Try

          Return retVal
          End Function

          Private Function FormatCurrency2 (ByVal CurString As String) As Decimal
          Dim tmp As String = "", x As String
          Dim t As Integer
          For t = 1 To Len(CurString)
          x = Strings.Mid(Cur String, t, 1)
          If IsNumeric(x) OrElse x = "." Then
          tmp &= x
          End If
          Next

          If Len(tmp) 0 Then
          Return CDec(tmp)
          Else
          Return 0D
          End If
          End Function

          If you have few currencies in your list, you could add these currencies
          to the FormatCurrency function.

          Please note that if you have to format many currencies, this might
          become a slowdown, if most currency strings are "$".
          Let's say, you have 100 currency strings. 90 of them are $, the other 10
          are EUR. If you add "EUR" to FormatCurrency, no matter what currency you
          have, it will always try to replace "EUR" - meaning in 90 attempts this
          will fail (as it is $).

          This is where FormatCurrency2 comes in. Let's say, you have a "EUR"
          currency string. FormatCurrency will fail to replace it (as it just
          replaces "$"). When trying to convert it to Decimal, it will branch to
          the Catch statement and call FormatCurrency2 .
          FormatCurrency2 will check which characters in the string are numeric
          (0...9) or decimal point and then concatenate them. After that it will
          convert it to Decimal or if there were no numeric values return 0.0.

          But there is one catch (at present): If your Windows is set up to use
          the "," instead of the "." for decimals, you will receive wrong values.
          3.33 becomes 333. Therefore, you would have to replace the "." with ","
          Just uncomment the Replace statement in FormatCurrency to do that.

          If you have many different currencies to deal with, then perhaps
          FormatCurrency3 is something for you:

          Private Function FormatCurrency3 (ByVal CurString As String) As Decimal
          Dim Currencies() As String = {"$", "EUR", "€", "¥", "£", "GBP", "RMB"}
          Dim tmp As String = CurString
          Dim retVal As Decimal
          Dim t As Integer

          'Uncomment the following statement if you use "," for decimal.
          'tmp = Replace(tmp, ".", ",")

          For t = 0 To UBound(Currenci es)
          If InStr(tmp, Currencies(t)) 0 Then
          tmp = Replace(tmp, Currencies(t), "")
          Exit For
          End If
          Next

          Try
          retVal = CDec(tmp)
          Catch
          retVal = FormatCurrency2 (tmp)
          End Try

          Return retVal
          End Function

          For an additional speed increase, I would put the Currencies array on
          class level so that the array is not created over and over again.
          The difference to the previous approach is that in the For...Next loop
          it first checks if a currency is used to avoid an unneccessary Replace
          attempt. Also here, in case the attempt to convert it to decimal fails,
          the string will be handed over to FormatCurrency2 .

          Best regards,

          Martin

          On 04.08.2008 08:58, TC wrote:
          Hey All,
          >
          I have strings in currency. For example: "$1.29"
          >
          What is the best way to convert them to decimal formatted like: "1.29"
          >
          Thanks,
          >
          TC
          >
          >

          Comment

          • Cor Ligthert[MVP]

            #6
            Re: String Conversion Question

            TC

            I hope you don't mind I took my own culture sign and decimal

            Dim deci As Decimal = CDec("€ 1,29")

            Cor







            "TC" <getmyemails2@y ahoo.comschreef in bericht
            news:e2eEH1c9IH A.4004@TK2MSFTN GP03.phx.gbl...
            Hey All,
            >
            I have strings in currency. For example: "$1.29"
            >
            What is the best way to convert them to decimal formatted like: "1.29"
            >
            Thanks,
            >
            TC
            >

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: String Conversion Question

              TC wrote:
              I tried Decimal.Parse and it's throwing me an error stating that the string
              is not in the correct format.
              >
              Specify NumberStyles.Cu rrency in the call to Parse, and specify the
              culture if the default culture doesn't match the currency.

              Dim c As Decimal = Decimal.Parse(" $1.29", NumberStyles.Cu rrency,
              CultureInfo.Get CultureInfo("en-us"))

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                #8
                Re: String Conversion Question

                TC wrote:
                Decimal.Parse was also giving me a number with 4 decimals (i.e. adding 2
                zeroes).
                No, it doesn't. A Decimal value doesn't contain any information about
                the format, so you specified (unknowingstly, probably implicitly) the
                number of decimals when formatting the value for display.

                --
                Göran Andersson
                _____
                Göran Anderssons privata hemsida.

                Comment

                • \(O\)enone

                  #9
                  Re: String Conversion Question

                  Göran Andersson wrote:
                  No, it doesn't. A Decimal value doesn't contain any information about
                  the format, so you specified (unknowingstly, probably implicitly) the
                  number of decimals when formatting the value for display.
                  Actually that's not correct, the implementation of Decimal stores the number
                  of decimal places.

                  For example:

                  \\\
                  Dim d1 As Decimal
                  Dim d2 As Decimal

                  d1 = Decimal.Parse(" 1.23")
                  d2 = Decimal.Parse(" 1.2300")

                  MsgBox(d1.ToStr ing)
                  MsgBox(d2.ToStr ing)
                  ///

                  ...displays:

                  1.23
                  1.2300

                  This certainly caught me out the first time I saw it...

                  --

                  (O)enone



                  Comment

                  • TC

                    #10
                    Re: String Conversion Question


                    OK. So then whay would something like "$1.23" throw an error when trying to
                    parse or why would it add 2 zeroes?

                    Perhaps better asked, again, is there a quick way to take a currency string
                    and parse out the decimal?

                    Perhaps regular expressions?



                    "(O)enone" <oenone@nowhere .comwrote in message
                    news:g76fk7$t9i $1@aioe.org...
                    Göran Andersson wrote:
                    >No, it doesn't. A Decimal value doesn't contain any information about
                    >the format, so you specified (unknowingstly, probably implicitly) the
                    >number of decimals when formatting the value for display.
                    >
                    Actually that's not correct, the implementation of Decimal stores the
                    number of decimal places.
                    >
                    For example:
                    >
                    \\\
                    Dim d1 As Decimal
                    Dim d2 As Decimal
                    >
                    d1 = Decimal.Parse(" 1.23")
                    d2 = Decimal.Parse(" 1.2300")
                    >
                    MsgBox(d1.ToStr ing)
                    MsgBox(d2.ToStr ing)
                    ///
                    >
                    ..displays:
                    >
                    1.23
                    1.2300
                    >
                    This certainly caught me out the first time I saw it...
                    >
                    --
                    >
                    (O)enone
                    >
                    >
                    >

                    Comment

                    • TC

                      #11
                      Re: String Conversion Question

                      Hey All,

                      Using Convert.ToStrin g in conjunction with the cold CDec function worked.
                      For example:

                      Dim myVal as String

                      myVal = "$1.23"

                      myVal = Convert.ToStrin g(CDec(myVal))

                      Only one line of code.

                      Anyone have a better suggestion / solution, by all means, please advise.




                      "(O)enone" <oenone@nowhere .comwrote in message
                      news:g76fk7$t9i $1@aioe.org...
                      Göran Andersson wrote:
                      >No, it doesn't. A Decimal value doesn't contain any information about
                      >the format, so you specified (unknowingstly, probably implicitly) the
                      >number of decimals when formatting the value for display.
                      >
                      Actually that's not correct, the implementation of Decimal stores the
                      number of decimal places.
                      >
                      For example:
                      >
                      \\\
                      Dim d1 As Decimal
                      Dim d2 As Decimal
                      >
                      d1 = Decimal.Parse(" 1.23")
                      d2 = Decimal.Parse(" 1.2300")
                      >
                      MsgBox(d1.ToStr ing)
                      MsgBox(d2.ToStr ing)
                      ///
                      >
                      ..displays:
                      >
                      1.23
                      1.2300
                      >
                      This certainly caught me out the first time I saw it...
                      >
                      --
                      >
                      (O)enone
                      >
                      >
                      >

                      Comment

                      Working...