Limiting decimals

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

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Limiting decimals

Post 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
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

thanks duff and other guy :p
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

That should have worked but it didnt

Code: Select all

<?php
$totalratio = ($totalwins / $total);
$totalratio2 = substr($totalratio, 1);  
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

up!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you had a look into [php_man]printf[/php_man]()?

Mac
Post Reply