Heim  >  Artikel  >  Backend-Entwicklung  >  PHP自定义函数round_pad_zero 小数位四舍五入并补零

PHP自定义函数round_pad_zero 小数位四舍五入并补零

WBOY
WBOYOriginal
2016-07-25 08:57:461322Durchsuche
  1. /**
  2. * 小数位四舍五入并补零
  3. * by bbs.it-home.org
  4. */
  5. function round_pad_zero($num, $precision)
  6. {
  7. if ($precision return round($num, 0);
  8. }
  9. $r_num = round($num, $precision);
  10. $num_arr = explode('.', "$r_num");
  11. if (count($num_arr) == 1) {
  12. return "$r_num" . '.' . str_repeat('0', $precision);
  13. }
  14. $point_str = "$num_arr[1]";
  15. if (strlen($point_str) $point_str = str_pad($point_str, $precision, '0');
  16. }
  17. return $num_arr[0] . '.' . $point_str;
  18. }
  19. //调用示例
  20. echo round_pad_zero(3223342.506, 4);
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn