Limiting the amount of digits in a value?
Posted: Wed Feb 15, 2006 11:30 am
I want to limit the number of digits a value can have, how could I go about doing that?
Here is my code that converts bytes to different values:
The value of MB always ends up having around 20 decimal places, how can I cut it down to 2 decimal places? Thanks.
Here is my code that converts bytes to different values:
Code: Select all
<?php
function convert_from_bytes( $bytes, $to=NULL )
{
$float = floatval( $bytes );
switch( $to )
{
case 'Kb' : // Kilobit
$float = ( $float*8 ) / 1024;
break;
case 'b' : // bit
$float *= 8;
break;
case 'GB' : // Gigabyte
$float /= 1024;
case 'MB' : // Megabyte
$float /= 1024;
case 'KB' : // Kilobyte
$float /= 1024;
default : // byte
}
unset( $bytes, $to );
return( $float );
}
$filesize = disk_total_space("./uploads/");
$MB = convert_from_bytes( $filesize, 'MB' );
echo($MB);
?>