Page 1 of 1

Limiting decimals

Posted: Wed Feb 04, 2004 5:47 pm
by John Cartwright

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

Posted: Wed Feb 04, 2004 5:51 pm
by uberpolak

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.

Posted: Wed Feb 04, 2004 8:38 pm
by DuFF
or round:

Code: Select all

<?php
round($ratio, 3); 
?>
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
}
?>

Posted: Thu Feb 05, 2004 6:25 am
by John Cartwright
thanks duff and other guy :p

Posted: Thu Feb 05, 2004 2:31 pm
by John Cartwright
That should have worked but it didnt

Code: Select all

<?php
$totalratio = ($totalwins / $total);
$totalratio2 = substr($totalratio, 1);  
?>

Posted: Thu Feb 05, 2004 7:21 pm
by John Cartwright
up!

Posted: Fri Feb 06, 2004 3:05 am
by twigletmac
Have you had a look into [php_man]printf[/php_man]()?

Mac