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. C / C++ / MFC
  4. CString to double conversion.

CString to double conversion.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialhelpquestion
5 Posts 5 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.
  • U Offline
    U Offline
    User 4396598
    wrote on last edited by
    #1

    Hi all, I am trying to convert CString value to double value. see below

                   if(pos == 0)
    		{
    			assfile.ReadString(nextLine);
    			pos = nextLine.Find(\_T(","));
    			temp = nextLine.Left(pos);
    			dValue = \_wtof(temp);    // dValue is double variable
    			
    			if (dValue < minval)
    				minval = dValue;
    		}
    

    i am using _wtof() to convert string to double. The problem here is after conversion the double value has more decimal points every time. for example str = "234.555" after conversion the double value will be like this 234.55500005 i want the double value to be as it is in the string. how to resolve this? Thanks in advance. Regards, kumar

    hi

    C T _ 3 Replies Last reply
    0
    • U User 4396598

      Hi all, I am trying to convert CString value to double value. see below

                     if(pos == 0)
      		{
      			assfile.ReadString(nextLine);
      			pos = nextLine.Find(\_T(","));
      			temp = nextLine.Left(pos);
      			dValue = \_wtof(temp);    // dValue is double variable
      			
      			if (dValue < minval)
      				minval = dValue;
      		}
      

      i am using _wtof() to convert string to double. The problem here is after conversion the double value has more decimal points every time. for example str = "234.555" after conversion the double value will be like this 234.55500005 i want the double value to be as it is in the string. how to resolve this? Thanks in advance. Regards, kumar

      hi

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You can't control that. This is due to the precision of the double which is not perfect. I suggest you google about "floating point precision" to learn more about the subject (see this[^] for instance).

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • U User 4396598

        Hi all, I am trying to convert CString value to double value. see below

                       if(pos == 0)
        		{
        			assfile.ReadString(nextLine);
        			pos = nextLine.Find(\_T(","));
        			temp = nextLine.Left(pos);
        			dValue = \_wtof(temp);    // dValue is double variable
        			
        			if (dValue < minval)
        				minval = dValue;
        		}
        

        i am using _wtof() to convert string to double. The problem here is after conversion the double value has more decimal points every time. for example str = "234.555" after conversion the double value will be like this 234.55500005 i want the double value to be as it is in the string. how to resolve this? Thanks in advance. Regards, kumar

        hi

        T Offline
        T Offline
        transoft
        wrote on last edited by
        #3

        Did you try:

        double dValue = 234.55500005;
        long lValue = dValue * 1000;
        dValue = lValue / 1000;

        T 1 Reply Last reply
        0
        • U User 4396598

          Hi all, I am trying to convert CString value to double value. see below

                         if(pos == 0)
          		{
          			assfile.ReadString(nextLine);
          			pos = nextLine.Find(\_T(","));
          			temp = nextLine.Left(pos);
          			dValue = \_wtof(temp);    // dValue is double variable
          			
          			if (dValue < minval)
          				minval = dValue;
          		}
          

          i am using _wtof() to convert string to double. The problem here is after conversion the double value has more decimal points every time. for example str = "234.555" after conversion the double value will be like this 234.55500005 i want the double value to be as it is in the string. how to resolve this? Thanks in advance. Regards, kumar

          hi

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          As Cedric Moonen said, you cannot control this. But you can always choose what you want to be displayed by formatting the data. For example, printf("%.3f", dValue); will only show you 3 decimal digits. The same formatting can also be used with CStringT::Format and other APIs like StringCchPrintf.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          1 Reply Last reply
          0
          • T transoft

            Did you try:

            double dValue = 234.55500005;
            long lValue = dValue * 1000;
            dValue = lValue / 1000;

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            I doubt!

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
            Never mind - my own stupidity is the source of every "problem" - Mixture

            cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

            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