Limiting the amount of digits in a value?

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
03barbosd
Forum Newbie
Posts: 5
Joined: Sat Feb 11, 2006 4:32 pm

Limiting the amount of digits in a value?

Post 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.
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

round( $num, 2 );
03barbosd
Forum Newbie
Posts: 5
Joined: Sat Feb 11, 2006 4:32 pm

Post by 03barbosd »

Ahh, but I just want the decimal places to be limited, not the number. Perhaps I should try the explode() function?
03barbosd
Forum Newbie
Posts: 5
Joined: Sat Feb 11, 2006 4:32 pm

Post by 03barbosd »

Sorry I misunderstood the function you posted, thanks for the code.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Round does limit the decimal places
Post Reply