Coercion of a String Into a Double Doesnt work (??!!)

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

    Coercion of a String Into a Double Doesnt work (??!!)

    I've been scratching my head for weeks to understand why some code doesnt
    work for me.
    here is what i have:

    dim sVal as string = "13.2401516 "
    dim x as double

    x = sVal

    debug.writeline ( x)

    Now on my system (English WinXp proSP3) the debug line
    is as follows
    13.2401516

    However, when a user in a different locale (different localized version of
    Windows) the debug line prints
    132401516
    The same number only without the decimal. Can anyone shed some light on why
    this happanes? is it because the user may have a

    different format for numbers and the coercion is done according to that
    format? How can i ensure that i get the proper conversion?



    AGP


  • Bill McCarthy

    #2
    Re: Coercion of a String Into a Double Doesnt work (??!!)

    Hi AGP,

    The reason is in some locales the . is used as a thousands separator much
    like how , is used in others, e.g 1,000 is the same as 1.000 depending on
    the locale.

    To take explicit control of how the Double is parsed used the double Parse
    method, e.g:

    x = Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)




    "AGP" <sindizzy.pak@s ofthome.netwrot e in message
    news:qDpsk.1931 2$mh5.9523@nlpi 067.nbdc.sbc.co m...
    I've been scratching my head for weeks to understand why some code doesnt
    work for me.
    here is what i have:
    >
    dim sVal as string = "13.2401516 "
    dim x as double
    >
    x = sVal
    >
    debug.writeline ( x)
    >
    Now on my system (English WinXp proSP3) the debug line
    is as follows
    13.2401516
    >
    However, when a user in a different locale (different localized version of
    Windows) the debug line prints
    132401516
    The same number only without the decimal. Can anyone shed some light on
    why this happanes? is it because the user may have a
    >
    different format for numbers and the coercion is done according to that
    format? How can i ensure that i get the proper conversion?
    >
    >
    >
    AGP
    >
    >

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Coercion of a String Into a Double Doesnt work (??!!)

      AGP,

      Be aware that you should avoid hard coded decimimal pointers

      However, that is impossiblie in a document or a webpage.

      Have therefore a look at this.


      Cor

      "AGP" <sindizzy.pak@s ofthome.netschr eef in bericht
      news:qDpsk.1931 2$mh5.9523@nlpi 067.nbdc.sbc.co m...
      I've been scratching my head for weeks to understand why some code doesnt
      work for me.
      here is what i have:
      >
      dim sVal as string = "13.2401516 "
      dim x as double
      >
      x = sVal
      >
      debug.writeline ( x)
      >
      Now on my system (English WinXp proSP3) the debug line
      is as follows
      13.2401516
      >
      However, when a user in a different locale (different localized version of
      Windows) the debug line prints
      132401516
      The same number only without the decimal. Can anyone shed some light on
      why this happanes? is it because the user may have a
      >
      different format for numbers and the coercion is done according to that
      format? How can i ensure that i get the proper conversion?
      >
      >
      >
      AGP
      >
      >

      Comment

      • AGP

        #4
        Re: Coercion of a String Into a Double Doesnt work (??!!)

        The data that i am reading will be non-locale specific and will always be
        written
        as pure decimal. Example 13.58 is read as 13 and 58 hundreths. So if I read
        your link properly then I should just coerce the string before assiging it
        and that should
        give me a pure decimal number as follows:

        dim sVal as string = "13.2401516 "
        dim x as double
        x = CDbl( sVal )

        AGP

        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
        news:uN3R1BoBJH A.1012@TK2MSFTN GP06.phx.gbl...
        AGP,
        >
        Be aware that you should avoid hard coded decimimal pointers
        >
        However, that is impossiblie in a document or a webpage.
        >
        Have therefore a look at this.

        >
        Cor
        >
        "AGP" <sindizzy.pak@s ofthome.netschr eef in bericht
        news:qDpsk.1931 2$mh5.9523@nlpi 067.nbdc.sbc.co m...
        >I've been scratching my head for weeks to understand why some code doesnt
        >work for me.
        >here is what i have:
        >>
        >dim sVal as string = "13.2401516 "
        >dim x as double
        >>
        >x = sVal
        >>
        >debug.writelin e ( x)
        >>
        >Now on my system (English WinXp proSP3) the debug line
        >is as follows
        >13.2401516
        >>
        >However, when a user in a different locale (different localized version
        >of Windows) the debug line prints
        >132401516
        >The same number only without the decimal. Can anyone shed some light on
        >why this happanes? is it because the user may have a
        >>
        >different format for numbers and the coercion is done according to that
        >format? How can i ensure that i get the proper conversion?
        >>
        >>
        >>
        >AGP
        >>
        >>
        >
        >

        Comment

        • AGP

          #5
          Re: Coercion of a String Into a Double Doesnt work (??!!)

          Im using VS2005 and noticed that secondary option doesnt come up by default
          so what is the difference between
          Double.Parse(sV al, Globalization.N umberStyles.Flo at)

          and

          Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)

          AGP



          "Bill McCarthy" <Bill@localhost .comwrote in message
          news:7875B05D-D22A-4DCE-830D-7F5087165326@mi crosoft.com...
          Hi AGP,
          >
          The reason is in some locales the . is used as a thousands separator much
          like how , is used in others, e.g 1,000 is the same as 1.000 depending on
          the locale.
          >
          To take explicit control of how the Double is parsed used the double Parse
          method, e.g:
          >
          x = Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)
          >
          >
          >
          >
          "AGP" <sindizzy.pak@s ofthome.netwrot e in message
          news:qDpsk.1931 2$mh5.9523@nlpi 067.nbdc.sbc.co m...
          >I've been scratching my head for weeks to understand why some code doesnt
          >work for me.
          >here is what i have:
          >>
          >dim sVal as string = "13.2401516 "
          >dim x as double
          >>
          >x = sVal
          >>
          >debug.writelin e ( x)
          >>
          >Now on my system (English WinXp proSP3) the debug line
          >is as follows
          >13.2401516
          >>
          >However, when a user in a different locale (different localized version
          >of Windows) the debug line prints
          >132401516
          >The same number only without the decimal. Can anyone shed some light on
          >why this happanes? is it because the user may have a
          >>
          >different format for numbers and the coercion is done according to that
          >format? How can i ensure that i get the proper conversion?
          >>
          >>
          >>
          >AGP
          >>
          >>
          >

          Comment

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

            #6
            Re: Coercion of a String Into a Double Doesnt work (??!!)

            AGP wrote:
            Im using VS2005 and noticed that secondary option doesnt come up by default
            The double.Parse method has an overload that takes an IFormatProvider .
            You can use any class that implements that interface, including
            NumberFormatInf o, but you won't get a complete list of those classes in
            the intellisense.
            so what is the difference between
            Double.Parse(sV al, Globalization.N umberStyles.Flo at)
            >
            and
            >
            Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)
            >
            AGP
            The difference is that the first one still uses the default culture,
            which may not use a period as decimal separator. The InvariantInfo is
            culture independent and a period has been chosen as it's decimal separator.

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

            Comment

            • AGP

              #7
              Re: Coercion of a String Into a Double Doesnt work (??!!)

              ok here is what i have

              dim sVal1 as string = "13.2401516 "
              dim sVal2 as string = "53.2168183 "
              a = CDbl(sVal1)

              b = Double.Parse(sV al2, Globalization.N umberFormatInfo .InvariantInfo)

              and the debug line from the user

              132401516

              53,2168183

              He is using a German Windows XP version. In that locale the number 1300 and
              24/100 would be written 1300,24 so it seems to me that the second is the
              correct way to writre it as suggested. Double.Parse(sV al2,
              Globalization.N umberFormatInfo .InvariantInfo) seems to correctly interpret
              the number. But my question is...that is how the user sees the number that
              is calculated but internally is it still 53 and 2168183/10000000? In other
              words, if i pump that value to another process will it still be seen as 53
              and 2168183/10000000?

              Im going to install the German MUI and do some testing here but i think Im
              learning something that I never paid much attention to in the past.

              AGP


              "Göran Andersson" <guffa@guffa.co mwrote in message
              news:e9Ijok0BJH A.1228@TK2MSFTN GP02.phx.gbl...
              AGP wrote:
              >Im using VS2005 and noticed that secondary option doesnt come up by
              >default
              >
              The double.Parse method has an overload that takes an IFormatProvider . You
              can use any class that implements that interface, including
              NumberFormatInf o, but you won't get a complete list of those classes in
              the intellisense.
              >
              >so what is the difference between
              >Double.Parse(s Val, Globalization.N umberStyles.Flo at)
              >>
              >and
              >>
              >Double.Parse(s Val, Globalization.N umberFormatInfo .InvariantInfo)
              >>
              >AGP
              >
              The difference is that the first one still uses the default culture, which
              may not use a period as decimal separator. The InvariantInfo is culture
              independent and a period has been chosen as it's decimal separator.
              >
              --
              Göran Andersson
              _____
              http://www.guffa.com

              Comment

              • Cor Ligthert[MVP]

                #8
                Re: Coercion of a String Into a Double Doesnt work (??!!)

                Pure,

                Pure decimal, only 2% of the world population is using the dot as decimal
                seperator, the rest is using a comma.
                Even in at least one country where English is used is the comma the decimal
                seperator.

                Cor

                "AGP" <sindizzy.pak@s ofthome.netschr eef in bericht
                news:KuIsk.1893 0$cW3.14978@nlp i064.nbdc.sbc.c om...
                The data that i am reading will be non-locale specific and will always be
                written
                as pure decimal. Example 13.58 is read as 13 and 58 hundreths. So if I
                read
                your link properly then I should just coerce the string before assiging it
                and that should
                give me a pure decimal number as follows:
                >
                dim sVal as string = "13.2401516 "
                dim x as double
                x = CDbl( sVal )
                >
                AGP
                >
                "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                news:uN3R1BoBJH A.1012@TK2MSFTN GP06.phx.gbl...
                >AGP,
                >>
                >Be aware that you should avoid hard coded decimimal pointers
                >>
                >However, that is impossiblie in a document or a webpage.
                >>
                >Have therefore a look at this.
                >http://www.vb-tips.com/Cultures.aspx
                >>
                >Cor
                >>
                >"AGP" <sindizzy.pak@s ofthome.netschr eef in bericht
                >news:qDpsk.193 12$mh5.9523@nlp i067.nbdc.sbc.c om...
                >>I've been scratching my head for weeks to understand why some code
                >>doesnt work for me.
                >>here is what i have:
                >>>
                >>dim sVal as string = "13.2401516 "
                >>dim x as double
                >>>
                >>x = sVal
                >>>
                >>debug.writeli ne ( x)
                >>>
                >>Now on my system (English WinXp proSP3) the debug line
                >>is as follows
                >>13.2401516
                >>>
                >>However, when a user in a different locale (different localized version
                >>of Windows) the debug line prints
                >>132401516
                >>The same number only without the decimal. Can anyone shed some light on
                >>why this happanes? is it because the user may have a
                >>>
                >>different format for numbers and the coercion is done according to that
                >>format? How can i ensure that i get the proper conversion?
                >>>
                >>>
                >>>
                >>AGP
                >>>
                >>>
                >>
                >>
                >
                >

                Comment

                • Andrew Morton

                  #9
                  Re: Coercion of a String Into a Double Doesnt work (??!!)

                  Cor Ligthert[MVP] wrote:
                  Pure decimal, only 2% of the world population is using the dot as
                  decimal seperator, the rest is using a comma.
                  Cite? I present to you the countries of China and India, which I suggest
                  between them make up somewhat more than 2% of the world's population ;-)



                  Andrew


                  Comment

                  • Bruce W. Roeser

                    #10
                    Re: Coercion of a String Into a Double Doesnt work (??!!)

                    Suggest y'all use TryParse() rather than Parse(). The former will return a
                    zero value if the input is bad whereas TryParse() will return a zero value
                    but not throw an exception.

                    -bwr-

                    "AGP" <sindizzy.pak@s ofthome.netwrot e in message
                    news:YE2tk.1156 5$vn7.1300@flpi 147.ffdc.sbc.co m...
                    ok here is what i have
                    >
                    dim sVal1 as string = "13.2401516 "
                    dim sVal2 as string = "53.2168183 "
                    a = CDbl(sVal1)
                    >
                    b = Double.Parse(sV al2, Globalization.N umberFormatInfo .InvariantInfo)
                    >
                    and the debug line from the user
                    >
                    132401516
                    >
                    53,2168183
                    >
                    He is using a German Windows XP version. In that locale the number 1300
                    and 24/100 would be written 1300,24 so it seems to me that the second is
                    the correct way to writre it as suggested. Double.Parse(sV al2,
                    Globalization.N umberFormatInfo .InvariantInfo) seems to correctly interpret
                    the number. But my question is...that is how the user sees the number that
                    is calculated but internally is it still 53 and 2168183/10000000? In other
                    words, if i pump that value to another process will it still be seen as 53
                    and 2168183/10000000?
                    >
                    Im going to install the German MUI and do some testing here but i think Im
                    learning something that I never paid much attention to in the past.
                    >
                    AGP
                    >
                    >
                    "Göran Andersson" <guffa@guffa.co mwrote in message
                    news:e9Ijok0BJH A.1228@TK2MSFTN GP02.phx.gbl...
                    >AGP wrote:
                    >>Im using VS2005 and noticed that secondary option doesnt come up by
                    >>default
                    >>
                    >The double.Parse method has an overload that takes an IFormatProvider .
                    >You can use any class that implements that interface, including
                    >NumberFormatIn fo, but you won't get a complete list of those classes in
                    >the intellisense.
                    >>
                    >>so what is the difference between
                    >>Double.Parse( sVal, Globalization.N umberStyles.Flo at)
                    >>>
                    >>and
                    >>>
                    >>Double.Parse( sVal, Globalization.N umberFormatInfo .InvariantInfo)
                    >>>
                    >>AGP
                    >>
                    >The difference is that the first one still uses the default culture,
                    >which may not use a period as decimal separator. The InvariantInfo is
                    >culture independent and a period has been chosen as it's decimal
                    >separator.
                    >>
                    >--
                    >Göran Andersson
                    >_____
                    >http://www.guffa.com
                    >
                    >

                    Comment

                    Working...