
|
View Full Version : Simple PHP Decimal Help
Mr_Predator 09-28-2002, 10:14 AM PHP Help. Got a load of variables with numbers such as 9.95, and want to * it by lets say 8, but only want the output to be to 2 decimal place (min and max so that if the numbers 45.4, it would be 45.40) for use with numbers with prices .etc. Anyone gets what I mean please help :), tried using the round() command but it doesnt add like the extra digit onto the end if theres only 1 deicmal place available.
michaeln 09-28-2002, 10:47 AM This will do what you want:
$price = round($price, 2);
Mr_Predator 09-28-2002, 10:57 AM Nope that what im currently trying to do, ill give you some example code...
$price_monthly = 4.95;
$price_biannually = round($price_monthly * 6, 2);
echo $price_biannually;
This works great BUT, this returns 29.7, but I want 29.70 instead, so its great for some but others it will miss the second decimal, as theres no need, but when using prices/currency .etc you need 2 decimal places, no more and no less :(.
Any Ideas?
Thanks.
michaeln 09-28-2002, 11:11 AM Well I suppose I get proven wrong...
;)
Mr_Predator 09-28-2002, 11:12 AM 4.2.3
michaeln 09-28-2002, 11:15 AM Yes that is what I am using and it seems that no longer works as my script is not working now either. Hrmm off to find another way to do it. Will let you know what I find...
Regards,
Michael
michaeln 09-28-2002, 11:20 AM I found where someong wrote this and posted it on php.net. There has to be a built in function though so we don't have to use this...
// splits a string and inserts commas and puts it into
// the common Money format 1,234.56
// Taken from cwe7g's comma integer script
// Ken Cochane
// http://www.PopcornMonsters.com
// 06-20-01
// Version 1.0
function to_money($string)
{
//look for commas in the string and remove them.
$string = preg_replace("|\,|","",$string);
//split the string into two First and Second
// First is before decimal, second is after First.Second
$Full = split("[\.]",$string);
$Count = count($Full);
if($Count > 1)
{
$First = $Full[0];
$Second = $Full[1];
$NumCents = strlen($Second);
if($NumCents == 2)
{
//do nothing already at correct length
}
else if($NumCents < 2)
{
//add an extra zero to the end
$Second = $Second . "0";
}
else if($NumCents > 2)
{
//either string off the end digits or round up
// I say string everything but the first 3 digits and then round
// since it is rare that anything after 3 digits effects the round
// you can change if you need greater accurcy, I don't so I didn't
// write that into the code.
$Temp = substr($Second,0,3);
$Rounded = round($Temp,-1);
$Second = substr($Rounded,0,2);
}
}
else
{
//there was no decimal on the end so add to zeros
$First = $Full[0];
$Second = "00";
}
$length = strlen($First);
if( $length <= 3 )
{
//To Short to add a comma
$string = $First . "." . $Second;
return $string;
}
else
{
$loop_count = intval( ( $length / 3 ) );
$section_length = -3;
for( $i = 0; $i < $loop_count; $i++ )
{
$sections[$i] = substr( $First, $section_length, 3 );
$section_length = $section_length - 3;
}
$stub = ( $length % 3 );
if( $stub != 0 )
{
$sections[$i] = substr( $First, 0, $stub );
}
$Done = implode( ",", array_reverse( $sections ) );
$Done = $Done . "." . $Second;
return $Done;
}
}
Mr_Predator 09-28-2002, 11:24 AM Wow that long =/, wanna cut it up for me so only need the 00 and 0 bit ;), guess i could live with working it out though
michaeln 09-28-2002, 11:34 AM LOL
I found a simpler answer....
$formatted = sprintf("%01.2f", $money);
That works... I promise.... LOL
Mr_Predator 09-28-2002, 11:52 AM woohoo thanks alot :), if this were real life and you were female id kiss your sweet ass.
michaeln 09-28-2002, 12:04 PM ROFL
No problem...
tapster 09-28-2002, 11:33 PM be aware that sprintf can have different results with regard to rounding on different OS's
just had this problem with runing a script I developed on my local box rounding nicely, but on the solaris production box it didn't!
|