leony Posted August 13, 2008 Share Posted August 13, 2008 Hi, Can anybody tell me why the following code outputs different results: <? $number = 2.16/0.36; echo "$number<br>"; echo ceil($number); ?> Output: 6 7 Link to comment https://forums.phpfreaks.com/topic/119543-ceil-help/ Share on other sites More sharing options...
ignace Posted August 13, 2008 Share Posted August 13, 2008 haha funny i guess this is because the basic behavior in converting from float to integer is the removal of decimals (i.e. floor()) ceil() probably check if the variable contains decimals and converts to integer and adds 1 Link to comment https://forums.phpfreaks.com/topic/119543-ceil-help/#findComment-615905 Share on other sites More sharing options...
Barand Posted August 13, 2008 Share Posted August 13, 2008 echo ceil(4/2); // 2, as expected. Floating point math rarely gives an exact value unless the fractional divisors are multiples of 2, (1/4, 3/4, 7/16, 23/128 etc) so if the result of your division is really 6.0000000000000001 then ceil() rounds up Link to comment https://forums.phpfreaks.com/topic/119543-ceil-help/#findComment-615941 Share on other sites More sharing options...
leony Posted August 14, 2008 Author Share Posted August 14, 2008 So what is the solution in this situation? ---------------- I think I have found it: To use number_format() <? $number = 2.16/0.36; $number = number_format($number,2); echo "$number<br>"; echo ceil($number); ?> Link to comment https://forums.phpfreaks.com/topic/119543-ceil-help/#findComment-616263 Share on other sites More sharing options...
corbin Posted August 14, 2008 Share Posted August 14, 2008 Edit: 2 seconds after posting, I realized that my solution wouldn't work. Link to comment https://forums.phpfreaks.com/topic/119543-ceil-help/#findComment-616268 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.