fork download
  1. <?php
  2. function myRound($number) {
  3. $magnitude = abs($number);
  4. if( $magnitude < 1000) $precision = -2;
  5. elseif( $magnitude < 10000) $precision = -3;
  6. else $precision = -4;
  7.  
  8. return round($number,$precision);
  9. }
  10.  
  11. $numbers = [
  12. 34 => 0,
  13. 89 => 100,
  14. 421 => 400,
  15. 561 => 600,
  16. 4421 => 4000,
  17. 6701 => 7000,
  18. 45000 => 50000,
  19. 91000 => 90000,
  20. 132000 => 130000
  21. ];
  22. foreach( $numbers as $input => $expected) {
  23. if( myRound($input) != $expected) {
  24. echo "FAIL: ".$input." returned ".myRound($input).", ".$expected." expected.\n";
  25. }
  26. }
  27. echo "Test ran successfully!";
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Test ran successfully!