alexguz79 Posted July 19, 2010 Share Posted July 19, 2010 Hey everybody... i am creating a website for the softball league i play in... and i have the batting average display with this code: echo round(($totalhits."") / ($abs.""),3) that takes the total hits and the total at bats and give me the average... the 3 is to limit the result to 3 figures insted of all the numbers in case of 0.333333333 the thing is i want to eliminate the 0 at the left of the dot... so it displays .333 insted of 0.333 the other thing is i want to display the complete number in case it's a round one... .400 insted of .4 thanks Quote Link to comment https://forums.phpfreaks.com/topic/208222-problem-echoing-average/ Share on other sites More sharing options...
Jessica Posted July 19, 2010 Share Posted July 19, 2010 Try number_format() Quote Link to comment https://forums.phpfreaks.com/topic/208222-problem-echoing-average/#findComment-1088342 Share on other sites More sharing options...
F1Fan Posted July 19, 2010 Share Posted July 19, 2010 jesirose is right, number_format is your best bet, but that will only answer the second question. It will round and display X number of decimals. As for displaying the zero to the left of the decimal, that you'll have to get creative. This should do it: $number = 0.3; list($leftOfDecimal, $rightOfDecimal) = explode('.', round($number, 3)); if ($leftOfDecimal == 0){ echo '.' . str_pad($rightOfDecimal, 3, STR_PAD_RIGHT); } else{ echo number_format($number, 3, '.', ','); } Quote Link to comment https://forums.phpfreaks.com/topic/208222-problem-echoing-average/#findComment-1088345 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.