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 float conversion

CString to float conversion

Scheduled Pinned Locked Moved C / C++ / MFC
question
2 Posts 2 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.
  • T Offline
    T Offline
    tagopi
    wrote on last edited by
    #1

    Hello everybody, I am trying to convert CString to float using atof(). but its not giving the exact value. for eg.

       CString strVal;
       GetDlgItem(IDC\_MAXLAYER)->GetWindowText(strVal);
       float mVal = atof(strVal);
    

    IDC_MAXLAYER is edit box and in that, i am having the value 386.080, but after atof() conversion, mVal shows 386.07999 is that possible to get exact 386.080 by converting? Thanks in advance. A. Gopinath.

    P 1 Reply Last reply
    0
    • T tagopi

      Hello everybody, I am trying to convert CString to float using atof(). but its not giving the exact value. for eg.

         CString strVal;
         GetDlgItem(IDC\_MAXLAYER)->GetWindowText(strVal);
         float mVal = atof(strVal);
      

      IDC_MAXLAYER is edit box and in that, i am having the value 386.080, but after atof() conversion, mVal shows 386.07999 is that possible to get exact 386.080 by converting? Thanks in advance. A. Gopinath.

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      Floating point numbers can not represent all real numbers accurately. Generally speaking: floating point numbers have a precision that is the greatest when the value is near zero. As soon as the whole part of the floating point number contains more and more digits, the fraction part quickly becomes super-inaccurate. Try entering numbers to your editbox like "234224.34654" into your editbox, you will see that the digits in the whole part will "steal" the precision from the fraction part because the value of this number is quite "far from zero". Read some more about floating point numbers: http://blogs.msdn.com/b/dwayneneed/archive/2010/05/07/fun-with-floating-point.aspx[^]. http://en.wikipedia.org/wiki/IEEE_754[^] People tend to use floating point numbers as a golden hammer when they don't know about its dark side. Sometimes its better to use just integers or fixed point arithmetic.

      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