You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
1.5 KiB

  1. <?php
  2. namespace Kuxin\Helper;
  3. /**
  4. * Class Num
  5. *
  6. * @package Kuxin\Helper
  7. * @author Pakey <pakey@qq.com>
  8. */
  9. class Math
  10. {
  11. /**
  12. * 取子id
  13. *
  14. * @param $id
  15. * @return float
  16. */
  17. public static function subid($id)
  18. {
  19. $id = (int)$id;
  20. return floor($id / 1000);
  21. }
  22. /**
  23. * 取子id
  24. *
  25. * @param $id
  26. * @return float
  27. */
  28. public static function subIdPlus($id)
  29. {
  30. $id = (int)$id;
  31. return Ceil($id / 1000);
  32. }
  33. /**
  34. * 文件大小格式化
  35. *
  36. * @param integer $size 初始文件大小,单位为byte
  37. * @return string 格式化后的文件大小和单位数组,单位为byte、KB、MB、GB、TB
  38. */
  39. public static function fileSizeFormat($size = 0, $dec = 2)
  40. {
  41. if(!$size){
  42. return '未知';
  43. }
  44. $unit = ["B", "KB", "MB", "GB", "TB", "PB"];
  45. $pos = 0;
  46. while ($size >= 1024) {
  47. $size /= 1024;
  48. $pos++;
  49. }
  50. $result['size'] = round($size, $dec);
  51. $result['unit'] = $unit[$pos];
  52. return $result['size'] . $result['unit'];
  53. }
  54. /**
  55. * @param mixed $num 科学计数法字符串 如 2.1E-5
  56. * @param int $double 小数点保留位数 默认5位
  57. * @return string
  58. */
  59. public static function ScToNum($num, $double = 5)
  60. {
  61. if (false !== stripos($num, "e")) {
  62. $a = explode("e", strtolower($num));
  63. return bcmul($a[0], bcpow(10, $a[1], $double), $double);
  64. }
  65. return $num;
  66. }
  67. }