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 Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/ 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 Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516748 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. Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516749 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 Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516752 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. Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516755 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. Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516758 Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 Anyone else got an idea? Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516759 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? Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516770 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); Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516779 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 Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516786 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... Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516791 Share on other sites More sharing options...
gple Posted April 14, 2008 Author Share Posted April 14, 2008 sorry guys. I figured it out. Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516795 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. Link to comment https://forums.phpfreaks.com/topic/101058-get-three-decimal-places/#findComment-516800 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.