-
July 2nd, 2004, 11:32 AM
#1
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 11:37 AM.
-
July 2nd, 2004, 11:46 AM
#2
I would do a
sscanf( k, "%lf", &a );
a+= 100;
sprintf( DigitArray, "%lf", a);
That should do it (not tested).
-
July 2nd, 2004, 11:55 AM
#3
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.
-
July 2nd, 2004, 12:44 PM
#4
?
Code:
_snprintf(DigitArray, 100, "%.01f", a);
-
July 2nd, 2004, 02:15 PM
#5
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.
-
July 2nd, 2004, 03:37 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|