gple Posted April 14, 2008 Share Posted April 14, 2008 $roster=mysql_query("select *, (hits/ab) as avg from softball_players, softball_rosters, softball_ostats where softball_ostats.roster_id=softball_rosters.id and softball_players.id=softball_rosters.player_id and softball_rosters.year='$year' order by avg desc"); $roster_num=mysql_numrows($roster); How do I get avg to appear in three decimal places? Not two as this is outputting Quote Link to comment Share on other sites More sharing options...
dark_mirage Posted April 14, 2008 Share Posted April 14, 2008 I don't totally understand what you mean, but i know you can format numbers with printf: printf ("%01.3f", $MyNumber); That should print to 3dp Haven't actually used this before though, so someone please correct my if i am wrong Quote Link to comment Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 When I output AVG. It rounds up to the second decimal place. I want to be able to output rounding up/down to the third decimal place. Quote Link to comment Share on other sites More sharing options...
dark_mirage Posted April 14, 2008 Share Posted April 14, 2008 Well in that case, try using the code i just said, replacing $MyNumber with your own variable obviously lol Quote Link to comment Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 I did and I am still getting it to two decimal places. I think something has to be done in the query where I coded (HITS/AB) as AVG. I think there is more coding involved here to have AVG go to three decimal places. Quote Link to comment Share on other sites More sharing options...
dark_mirage Posted April 14, 2008 Share Posted April 14, 2008 Well i don't know then sorry :-\ you'll have to wait for one of the guru's to check your post out, i doubt you'll have to wait long. Quote Link to comment Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 Anyone else got an idea? Quote Link to comment Share on other sites More sharing options...
atl_andy Posted April 14, 2008 Share Posted April 14, 2008 Could you limit the data the is entered into the database, so it is in the form you want when it is retrieved? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 If there's only two decimal places being shown without you doing anything, then it would suggest that the number can be shown exactly to two decimals - that is, can be written as a fraction with 100 as the denominator. Perhaps you wanted 3 decimals to be shown regardless? So an extra 0 would be shown? If so, you can use the number_format() function: echo number_format($var,3); Quote Link to comment Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 When AB=7 and H=3. Then AB/H should output .428. Instead its outputting .43. and then when I use your code it outputs .430 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 Im assuming you mean H/AB? Either way, thats odd. Perhaps try doing: select *, ROUND((hits/ab),3) as avg... Quote Link to comment Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 sorry guys. I figured it out. Quote Link to comment Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 Ah that was the code I was looking for. Now it works exactly the way I like it. Quote Link to comment 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.