Reputation: 10581
I am specifying a float as 0.1 and then rendering it to the browser with an php echo and it is giving the value 1.77175e-07.
What could be causing this very odd occurrence?
//Set reward
$reward = 0.1;
$question_result = mysql_query("SELECT question, answer, timelimit FROM trivia_questions ORDER BY RAND() LIMIT 1");
$question_array = mysql_fetch_array($question_result);
$question = $question_array['question'];
$answer = $question_array['answer'];
$timelimit = $question_array['timelimit'];
//Send response
echo "question,".$question.",".$answer.",".$reward.",".$timelimit.",".$question." (".$reward."L$ Reward)";
Upvotes: 0
Views: 118
Reputation: 48357
This should really be a comment - but it's too long.
I think that the other commentators are trying to explain that most computers don't store decimal floating point numbers - they are changed to binary and back again when required - which can lead to some rounding errors (e.g. 0.1 might become 0.1000013342...) but there is a MASSIVE difference between 0.1 and 0.000000177175 which is not a conversion error.
The title of the question implies you think that the number retrieved from the database is incorrect - but the code you've shown does not write 0.1 into the database - nor do you give any indication how it's being retrieved, nor the type of the database field in which it is stored.
If PHP and/or mysql were incapable of processing floating point numbers without any reliability I think someone would have noticed by now - there's a bug somewhere in your code.
Check the data type on the database. Check you are inserting a floating point value (not quoted). Use a different client to retrieve the value from the database.
It's very helpful to see code in questions - but it's all too easy to omit important stuff and to include a lot of irrelevant stuff. Next time try creating a minimal script to replicate the problem (e.g. in the case here - one which inserts a value into a table, and retrieves that value). You might want to have a Google for E.S.Raymond's Howto Ask Questions the Smart Way.
Upvotes: 1