Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Simple question on String to double

Simple question on String to double

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andreoli Carlo
    wrote on last edited by
    #1

    I have some problem in this simple passage:

    String ^string_num="9,99";	 
    double d_num1=Double::Parse(string_num);
    double d_num2=System::Convert::ToDouble(string_num);
    

    i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:

    L L Z 3 Replies Last reply
    0
    • A Andreoli Carlo

      I have some problem in this simple passage:

      String ^string_num="9,99";	 
      double d_num1=Double::Parse(string_num);
      double d_num2=System::Convert::ToDouble(string_num);
      

      i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Floating point (and by extension double) numbers in computers can only ever be approximations of their decimal value, as the number is stored in binary scientific notation, i.e. exponent and mantissa, rather than decimal numeric. If you want accuracy then you need to stick to integers. See here[^] for a full description.

      MVP 2010 - are they mad?

      A 1 Reply Last reply
      0
      • A Andreoli Carlo

        I have some problem in this simple passage:

        String ^string_num="9,99";	 
        double d_num1=Double::Parse(string_num);
        double d_num2=System::Convert::ToDouble(string_num);
        

        i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        ... or use the Decimal type. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


        1 Reply Last reply
        0
        • L Lost User

          Floating point (and by extension double) numbers in computers can only ever be approximations of their decimal value, as the number is stored in binary scientific notation, i.e. exponent and mantissa, rather than decimal numeric. If you want accuracy then you need to stick to integers. See here[^] for a full description.

          MVP 2010 - are they mad?

          A Offline
          A Offline
          Andreoli Carlo
          wrote on last edited by
          #4

          ok this make sense:), what i'll do is truncate after 2 decimal unit when visualizing the data to video thank to both of you

          L 1 Reply Last reply
          0
          • A Andreoli Carlo

            ok this make sense:), what i'll do is truncate after 2 decimal unit when visualizing the data to video thank to both of you

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            barbetto80 wrote:

            what i'll do is truncate after 2 decimal unit when visualizing the data

            Before taking this decision, make sure this will give you the results you need. For example using float/double for financial data is not a good idea as the roundings may cause loss of accuracy. This may not seem important with an example such as you showed above but it gets worse if you use the data in calculations and you can end up with significant errors in your results.

            MVP 2010 - are they mad?

            A 1 Reply Last reply
            0
            • A Andreoli Carlo

              I have some problem in this simple passage:

              String ^string_num="9,99";	 
              double d_num1=Double::Parse(string_num);
              double d_num2=System::Convert::ToDouble(string_num);
              

              i expect d_num1 and d_num2 to be 9,99....but from the watcher i clearly see they both are 9.9900000000000002 any help? :confused:

              Z Offline
              Z Offline
              zhushaolin2005
              wrote on last edited by
              #6

              try as following: String ^str="9.99"; System::Single d=Single::Parse(str);

              L 1 Reply Last reply
              0
              • Z zhushaolin2005

                try as following: String ^str="9.99"; System::Single d=Single::Parse(str);

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                This won't help, it will still have the accuracy problem.

                MVP 2010 - are they mad?

                1 Reply Last reply
                0
                • L Lost User

                  barbetto80 wrote:

                  what i'll do is truncate after 2 decimal unit when visualizing the data

                  Before taking this decision, make sure this will give you the results you need. For example using float/double for financial data is not a good idea as the roundings may cause loss of accuracy. This may not seem important with an example such as you showed above but it gets worse if you use the data in calculations and you can end up with significant errors in your results.

                  MVP 2010 - are they mad?

                  A Offline
                  A Offline
                  Andreoli Carlo
                  wrote on last edited by
                  #8

                  i know...but that is only a simple representation of a zoom factor application on an image....so i think i can show a truncated value because the user don't need the full value. But thanks for the advice

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups