Home > Backend Development > PHP Tutorial > PHP custom function round_pad_zero rounds decimal places and pads with zeros

PHP custom function round_pad_zero rounds decimal places and pads with zeros

WBOY
Release: 2016-07-25 08:57:46
Original
1401 people have browsed it
  1. /**
  2. * Decimal places rounded and zero padded
  3. * by bbs.it-home.org
  4. */
  5. function round_pad_zero($num, $precision)
  6. {
  7. if ($precision < 1) {
  8. return round($num, 0);
  9. }
  10. $r_num = round($num, $precision);
  11. $num_arr = explode('.', "$r_num");
  12. if (count($num_arr) == 1) {
  13. return "$r_num" . '.' . str_repeat('0', $precision);
  14. }
  15. $point_str = "$num_arr[1]";
  16. if (strlen($point_str) < $precision) {
  17. $point_str = str_pad($point_str, $precision, '0');
  18. }
  19. return $num_arr[0] . '.' . $point_str;
  20. }
  21. //调用示例
  22. echo round_pad_zero(3223342.506, 4);
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template