phppup Posted March 17, 2012 Share Posted March 17, 2012 Can someone clear up my confusion about FLOAT. I have data in a TINYINT field that displays whole numbers WITHOUT any decimal places. This does not format very nicely when there are ALSO decimalled values to be echoed: 1 1.5 2 2.5, etc. It can be a little confusing. The only way I've thought of to resolve the issue (if you know another, please share) is to add a decimal place to get: 1.0 1.5 2.0 2.5 etc. But I am unclear on WHERE to put the commands to get this result. Does it go into the actual DB? Or into my PHP script? Please help with a detailed example. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/259149-a-question-about-float/ Share on other sites More sharing options...
phppup Posted March 17, 2012 Author Share Posted March 17, 2012 And given my example of a ONE (or possibly two) place decimal, is there a benefit to using FLOAT versus DECIMAL? Quote Link to comment https://forums.phpfreaks.com/topic/259149-a-question-about-float/#findComment-1328523 Share on other sites More sharing options...
kicken Posted March 17, 2012 Share Posted March 17, 2012 You would format the numbers when you print them out so that they all have the same amount of digits after the decimal point. You can use number_format to do this. Quote Link to comment https://forums.phpfreaks.com/topic/259149-a-question-about-float/#findComment-1328574 Share on other sites More sharing options...
guinbRo Posted March 17, 2012 Share Posted March 17, 2012 You would format the numbers when you print them out so that they all have the same amount of digits after the decimal point. You can use number_format to do this. I'm curious, could you also type cast the numbers? Quote Link to comment https://forums.phpfreaks.com/topic/259149-a-question-about-float/#findComment-1328576 Share on other sites More sharing options...
kicken Posted March 17, 2012 Share Posted March 17, 2012 I'm curious, could you also type cast the numbers? If you want to display leading or tailing zeros (eg, 2.50 vs 2.5) then you have to cast the number to a string, apply the extra zeros, then output it. For displaying a number to a certain decimal point value, number_format will handle this easily. If you want leading zero's you'd use str_pad or sprintf. Quote Link to comment https://forums.phpfreaks.com/topic/259149-a-question-about-float/#findComment-1328588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.