Page 1 of 1

Limiting the amount of digits in a value?

Posted: Wed Feb 15, 2006 11:30 am
by 03barbosd
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:

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);
?>
The value of MB always ends up having around 20 decimal places, how can I cut it down to 2 decimal places? Thanks.

Posted: Wed Feb 15, 2006 11:32 am
by chrys
round( $num, 2 );

Posted: Wed Feb 15, 2006 11:34 am
by 03barbosd
Ahh, but I just want the decimal places to be limited, not the number. Perhaps I should try the explode() function?

Posted: Wed Feb 15, 2006 11:39 am
by 03barbosd
Sorry I misunderstood the function you posted, thanks for the code.

Posted: Wed Feb 15, 2006 11:42 am
by josh
Round does limit the decimal places