Posted By

coggla on 01/20/08


Tagged

numbers formatting rounding


Versions (?)

Numbers


 / Published in: PHP
 

Basics for formatting and generating numbers

  1. // Generate a random number
  2. srand ();
  3. $random_number = rand(0,10); //lower and higher ends of range
  4.  
  5. // Check whether one number is divisible by another as a whole number
  6. // Returns the remainder only - therefore a zero equals a whole number
  7.  
  8. $divisible = $number_to_check % $divisible_by;
  9.  
  10. // Formatting numbers
  11.  
  12. // Zero padded numbers
  13. $date = sprintf("%04d-%02d-%02d", $year, $month, $day);
  14.  
  15. // Setting decimal places
  16. $amount = sprintf("%01.2f", $total);
  17.  
  18. // Formatting a number with commas, no decimal places
  19. $num = number_format($num);
  20.  
  21. // Formatting a number with commas, 2 decimal places
  22. $num = number_format($num, 2);
  23.  
  24. // Round to the nearest .05
  25. $price = round(($amount/.05)*.05);
  26.  
  27. // Round down to nearest 10
  28. $result = ($num % 10 >= 10 ? ceil($num / 10)*10 : floor($num / 10)*10);
  29.  
  30. // Number of one hundredths round up
  31. $number = ceil($amount / 100);

Report this snippet  

You need to login to post a comment.