A problem of setting precison
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: A problem of setting precison

  1. #1
    Join Date
    Jul 2004
    Posts
    6

    A problem of setting precison

    I appreciate all the answers from you, thank you very much.

    I must stress, the program I wrote is a Win32 program, so I can not use C++, I just want to know if I can find a way to control the float value's precision using C instead of C++:

    I recently wrote a programme under Win32 environment, I have a problem which I can not solve independently.

    double a ;

    TCHAR *k = "152.3", DigitArray[ 100 ] ;

    a = strtod( k, NULL ) ; // convert a string to double.

    a + = 100 ;

    _gvct( a, 100, DigitArray ) ; //Convert double to string.

    TextOut( hdc, 0, 0, DigitArray, NULL ) ;

    The value of 'a' would be shown as 252.29999999999993, just becaue the default precision is 14 after the decimial point, I hope the value is 252.3 instead of 252.29999999999993, so I wish somebody can tell me a way to solve the above-mentioned problem.

    IMPORTANT:

    Only C language is acceptable, any methods based on C++ is

    not valid !

    Thank you very much!
    Last edited by ysapex; July 2nd, 2004 at 12:37 PM.

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,112
    I would do a
    sscanf( k, "%lf", &a );
    a+= 100;
    sprintf( DigitArray, "%lf", a);

    That should do it (not tested).

  3. #3
    Join Date
    Jul 2004
    Posts
    6

    Your way is not efficient!

    because it includes the operation of FILE read-out and write-in, I think it is not necessary at all.

  4. #4
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    ?
    Code:
    _snprintf(DigitArray, 100, "%.01f", a);

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,112

    Re: Your way is not efficient!

    Originally posted by ysapex
    because it includes the operation of FILE read-out and write-in, I think it is not necessary at all.
    where do you think is file i/o used?
    s[printf|scanf] are buffer related, i. e. memory related.
    Go read some books on the basics of C.

  6. #6
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457
    Probably just confused sscanf with fscanf.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center