C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2009, 06:01 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 2
atof conversion error

I realize this is probably a stupid question, I just don't use these routines much and thus am ... well, ignorant.

I call atof with a string of "18.69".

The result is 18.690000534.

Why ? Do I have to come up with a wrapper function if I am always working with figures out to two decimal places and want just that - just 18.69 ?
codeman is offline   Reply With Quote
Old 07-03-2009, 06:22 PM   #2
Registered User
 
Join Date: Sep 2006
Posts: 2,501
You can specify that the answer be printed out only to the first 2 decimal places:

Code:
dbl_number = 17.123456;
printf("dbl_number to two decimal places is: %.2f \n", dbl_number);
Adak is offline   Reply With Quote
Old 07-03-2009, 06:39 PM   #3
Registered User
 
Join Date: Jul 2009
Posts: 2
correction

Foolish me.

I'm assuming the conversion might be ok. It's just my IDE telling me the value is 18.690000534.

I assign 18.69 to a float value, then break, and place the mouse cursor over the value. The IDE (Borland C++ Builder version 5.0 Prodfessional) says it's 18.690000534.
codeman is offline   Reply With Quote
Old 07-03-2009, 11:10 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by codeman View Post
Foolish me.

I'm assuming the conversion might be ok. It's just my IDE telling me the value is 18.690000534.

I assign 18.69 to a float value, then break, and place the mouse cursor over the value. The IDE (Borland C++ Builder version 5.0 Prodfessional) says it's 18.690000534.
Very possible. It's highly unlikely the value of a float variable can ever actually be 18.69 exactly (it's certainly impossible if your machine follows the IEEE standard).
tabstop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quantum Random Bit Generator shawnt C++ Programming 62 06-18-2008 10:17 AM
Screwy Linker Error - VC2005 Tonto C++ Programming 5 06-19-2007 02:39 PM
more then 100errors in header hallo007 Windows Programming 20 05-13-2007 08:26 AM
Using VC Toolkit 2003 Noobwaker Windows Programming 8 03-13-2006 07:33 AM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM


All times are GMT -6. The time now is 05:53 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22