PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Feb 04, 2004 5:47 pm
Code: Select all
$calwins = 0;
$caltie = 0;
$calloss = 0;
// Setting variables from the table
while ( $row = mysql_fetch_array($result) ) {
$date = $rowї"date"];
$outcome = $rowї"outcome"];
$league = $rowї"league"];
if ($rowї'outcome'] == win) {
$calwins = $calwins + 1;
}elseif ($rowї'outcome'] == loss) {
$calloss = $calloss + 1;
}elseif ($rowї'outcome'] == tie) {
$caltie = $caltie + 1;
}
}
$caltotal = $calwins + $calloss + $caltie;
$ratio = ($calwins / $caltotal);
?>
How would i make the output 3 decimal places..... and also not show the 0 ie 0.234 ... wut i want is .234
uberpolak
Forum Contributor
Posts: 261 Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar
Post
by uberpolak » Wed Feb 04, 2004 5:51 pm
Code: Select all
<?php
number_format($ratio, 3);
?>
If you want to do a bunch of pain-in-the-ass work, you could drop the zero by converting to a string and doing a bunch of stuff, but there's probably a better way.
DuFF
Forum Contributor
Posts: 495 Joined: Tue Jun 24, 2003 7:49 pm
Location: USA
Post
by DuFF » Wed Feb 04, 2004 8:38 pm
or round:
to get rid of the zero you could do this:
Code: Select all
<?php
if($ratio < 1) //that means that it must be 0.XXX
{
$ratio = substr($ratio, 1); //this will take the first character out of a string
}
?>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Feb 05, 2004 2:31 pm
That should have worked but it didnt
Code: Select all
<?php
$totalratio = ($totalwins / $total);
$totalratio2 = substr($totalratio, 1);
?>
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Fri Feb 06, 2004 3:05 am
Have you had a look into [php_man]printf[/php_man]()?
Mac