PageRenderTime 6385ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/phunction/Math/Round.php

http://github.com/alixaxel/phunction
PHP | 45 lines | 31 code | 8 blank | 6 comment | 9 complexity | af8ce190e6a7871aa0558778a60a770b MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * The MIT License
  4. * http://creativecommons.org/licenses/MIT/
  5. *
  6. * Copyright (c) Alix Axel <alix.axel@gmail.com>
  7. **/
  8. class phunction_Math_Round extends phunction_Math
  9. {
  10. public function __construct()
  11. {
  12. }
  13. public static function Digit($number, $precision, $callback = 'round')
  14. {
  15. if (($precision != 0) && ($precision = abs($precision)))
  16. {
  17. return self::Multiple($number - $precision, pow(10, intval(log($precision, 10) + 1)), $callback) + $precision;
  18. }
  19. return self::Multiple($number, pow(10, strspn(strpbrk($precision, '0'), '0')), $callback);
  20. }
  21. public static function Multiple($number, $precision, $callback = 'round')
  22. {
  23. if (($precision != 0) && ($precision = abs($precision)))
  24. {
  25. return call_user_func($callback, $number / $precision) * $precision;
  26. }
  27. return 0;
  28. }
  29. public static function Significant($number, $precision, $callback = 'round')
  30. {
  31. if (($precision != 0) && ($precision = abs($precision)))
  32. {
  33. return self::Multiple($number, pow(10, intval(log($number, 10) + 1) - $precision), $callback);
  34. }
  35. return 0;
  36. }
  37. }