hamaldus Posted March 5, 2009 Share Posted March 5, 2009 Greetings! Im struggling getting the following working: I need to first strip down a decimal to one decimal. Fair enough, just use round($val,1). But after that, I need to remove the trailing zero, if there are any: 12.3444562 becomes 12.3 12.012 becomes 12 12.61 becomes 12.6 And so on. Is there an easy way to do this? Thanks for all help:) Quote Link to comment https://forums.phpfreaks.com/topic/148050-remove-trailing-zeroes-after-round/ Share on other sites More sharing options...
sasa Posted March 5, 2009 Share Posted March 5, 2009 ltrim(number_format($number,1),'0'); Quote Link to comment https://forums.phpfreaks.com/topic/148050-remove-trailing-zeroes-after-round/#findComment-777157 Share on other sites More sharing options...
Daniel0 Posted March 5, 2009 Share Posted March 5, 2009 round doesn't preserve the exact amount of decimals you specify, so round(12.012, 1) returns 12 exactly the way you want it to. Quote Link to comment https://forums.phpfreaks.com/topic/148050-remove-trailing-zeroes-after-round/#findComment-777221 Share on other sites More sharing options...
hamaldus Posted March 5, 2009 Author Share Posted March 5, 2009 Thanks for the replies, Daniel0 you are quite right. There was no problem. Quote Link to comment https://forums.phpfreaks.com/topic/148050-remove-trailing-zeroes-after-round/#findComment-777549 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.