float to CString conversion not exact

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YouPoP
    New Member
    • Jun 2007
    • 17

    float to CString conversion not exact

    For a reason i don't know, if i convert a float to a cstring, it does not convert to an exact value for all values except round numbers or 1/2 numbers;

    the following code (used to convert and later show as text in a MFC Document)
    Code:
    char buffer1[_CVTBUFSIZE];
    _gcvt(i_A11, 12, buffer1);
    CString SA11 = (CString) buffer1;
    output
    float i_A11 = 1,2 and once converted to Cstring SA11 = "1.20000004 768"
    float i_A11 = 1,0 and once converted to Cstring SA11 = "1."
    float i_A11 = 1,5 and once converted to Cstring SA11 = "1.5"

    thanks in advance for the help!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Read What every computer scientist should know about floating-point arithmetic.

    kind regards,

    Jos

    Comment

    Working...