BcHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\helpers;
  3. /**
  4. * bc 高精度库
  5. *
  6. * 四舍六入(银行家舍入)
  7. * round(1.2849, 2, PHP_ROUND_HALF_EVEN);
  8. *
  9. * Class BcHelper
  10. * @package common\helpers
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class BcHelper
  14. {
  15. /**
  16. * 将二个高精确度数字相除
  17. *
  18. * @param $dividend
  19. * @param $divisor
  20. * @param int $scale
  21. * @return string|null
  22. */
  23. public static function div($dividend, $divisor, $scale = 2)
  24. {
  25. return bcdiv($dividend, $divisor, $scale);
  26. }
  27. /**
  28. * 将二个高精确度数字相乘
  29. *
  30. * @param $dividend
  31. * @param $divisor
  32. * @param int $scale
  33. * @return string|null
  34. */
  35. public static function mul($dividend, $divisor, $scale = 2)
  36. {
  37. return bcmul($dividend, $divisor, $scale);
  38. }
  39. /**
  40. * 两个高精度数求余/取模
  41. *
  42. * @param $dividend
  43. * @param $divisor
  44. * @param int $scale
  45. * @return string|null
  46. */
  47. public static function mod($dividend, $divisor, $scale = 2)
  48. {
  49. return bcmod($dividend, $divisor, $scale);
  50. }
  51. /**
  52. * 将二个高精确度数字相加
  53. *
  54. * @param $left_operand
  55. * @param $right_operand
  56. * @param int $scale
  57. * @return string
  58. */
  59. public static function add($left_operand, $right_operand, $scale = 2)
  60. {
  61. return bcadd($left_operand, $right_operand, $scale);
  62. }
  63. /**
  64. * 将二个高精确度数字相减
  65. *
  66. * @param $left_operand
  67. * @param $right_operand
  68. * @param int $scale
  69. * @return string|double
  70. */
  71. public static function sub($left_operand, $right_operand, $scale = 2)
  72. {
  73. return bcsub($left_operand, $right_operand, $scale);
  74. }
  75. /**
  76. * 比较二个高精确度数字
  77. *
  78. * @param $left_operand
  79. * @param $right_operand
  80. * @param int $scale
  81. * @return string
  82. */
  83. public static function comp($left_operand, $right_operand, $scale = 2)
  84. {
  85. return bccomp($left_operand, $right_operand, $scale);
  86. }
  87. /**
  88. * 求一高精确度数字次方值
  89. *
  90. * @param $base
  91. * @param $exponent
  92. * @param int $scale
  93. * @return string
  94. */
  95. public static function pow($base, $exponent, $scale = 2)
  96. {
  97. return bcpow($base, $exponent, $scale);
  98. }
  99. /**
  100. * 求一高精确度数字次方值
  101. *
  102. * @param $operand
  103. * @param null $scale
  104. * @return string
  105. */
  106. public static function sqrt($operand, $scale = null)
  107. {
  108. return bcsqrt($operand, $scale);
  109. }
  110. /**
  111. * 设置所有bc数学函数的默认小数点保留位数
  112. *
  113. * @param $scale
  114. * @return bool
  115. */
  116. public static function scale($scale)
  117. {
  118. return bcscale($scale);
  119. }
  120. /**
  121. * 四舍五入
  122. *
  123. * @param $num
  124. * @param $scale
  125. * @return float
  126. */
  127. private static function round($num, $scale)
  128. {
  129. return round($num, $scale);
  130. }
  131. }
粤ICP备19079148号