round() 函数对浮点数进行四舍五入

语法:round(x,prec)

参数 描述

x 可选,规定要舍入的数字.

prec 可选,规定小数点后的位数.

说明:返回将 x 根据指定精度 prec(十进制小数点后数字的数目)进行四舍五入的结果,prec 也可以是负数或零(默认值).

提示和注释

注释:php默认不能正确处理类似 "12,300.2" 的字符串.

注释:prec 参数是在 php 4 中被引入的,实例代码如下:

  1. <?php
  2. */
  3. echo round(3.4); //输出3
  4. echo "<br>";
  5. echo round(3.5); //输出4
  6. echo "<br>";
  7. echo round(3.6); //输出4
  8. echo "<br>";
  9. echo round(3.6,0); //输出4
  10. echo "<br>";
  11. echo round(1.95583,2); //输出1.96
  12. echo "<br>";
  13. echo round(1241757,-3); //输出1242000
  14. echo "<br>";
  15. echo round(4.045,2); //输出4.05
  16. //开源代码phpfensi.com
  17. echo "<br>";
  18. echo round(4.055,2); //输出4.06
  19. //使用stdround函数代替php的round函数
  20. ?>
  21. <?php
  22. function stdround($num, $d=0)
  23. {
  24. return round($num + 0.0001 / pow(10, $d), $d);
  25. }
  26. echo "round(1.005,2)= ".round(1.005,2). "n ";
  27. echo "round(1.015,2)= ".round(1.015,2). "n ";
  28. echo "round(1.025,2)= ".round(1.025,2). "n ";
  29. echo "round(1.035,2)= ".round(1.035,2). "n ";
  30. echo "round(1.045,2)= ".round(1.045,2). "n ";
  31. echo "round(1.055,2)= ".round(1.055,2). "n ";
  32. echo "round(1.065,2)= ".round(1.065,2). "n ";
  33. echo "round(1.075,2)= ".round(1.075,2). "n ";
  34. echo "round(1.085,2)= ".round(1.085,2). "n ";
  35. echo "round(1.095,2)= ".round(1.095,2). "n ";
  36. echo "stdround(1.005,2)= ".stdround(1.005,2). "n ";
  37. echo "stdround(1.015,2)= ".stdround(1.015,2). "n ";
  38. echo "stdround(1.025,2)= ".stdround(1.025,2). "n ";
  39. echo "stdround(1.035,2)= ".stdround(1.035,2). "n ";
  40. echo "stdround(1.045,2)= ".stdround(1.045,2). "n ";
  41. echo "stdround(1.055,2)= ".stdround(1.055,2). "n ";
  42. echo "stdround(1.065,2)= ".stdround(1.065,2). "n ";
  43. echo "stdround(1.075,2)= ".stdround(1.075,2). "n ";
  44. echo "stdround(1.085,2)= ".stdround(1.085,2). "n ";
  45. echo "stdround(1.095,2)= ".stdround(1.095,2). "n ";
  46. $m=0.000000000000001;
  47. echo "n ";
  48. echo "round(1.005+{$m},2)= ".round(1.005+$m,2). "n ";
  49. echo "round(1.015+{$m},2)= ".round(1.015+$m,2). "n ";
  50. echo "round(1.025+{$m},2)= ".round(1.025+$m,2). "n ";
  51. echo "round(1.035+{$m},2)= ".round(1.035+$m,2). "n ";
  52. echo "round(1.045+{$m},2)= ".round(1.045+$m,2). "n ";
  53. echo "round(1.055+{$m},2)= ".round(1.055+$m,2). "n ";
  54. echo "round(1.065+{$m},2)= ".round(1.065+$m,2). "n ";
  55. echo "round(1.075+{$m},2)= ".round(1.075+$m,2). "n ";
  56. echo "round(1.085+{$m},2)= ".round(1.085+$m,2). "n ";
  57. echo "round(1.095+{$m},2)= ".round(1.095+$m,2). "n ";
  58. echo "round(1.005,2)= ".round(1.005,2). "n ";
  59. echo "round(1.015,2)= ".round(1.015,2). "n ";
  60. echo "round(1.025,2)= ".round(1.025,2). "n ";
  61. echo "round(1.035,2)= ".round(1.035,2). "n ";
  62. echo "round(1.045,2)= ".round(1.045,2). "n ";
  63. echo "round(1.055,2)= ".round(1.055,2). "n ";
  64. echo "round(1.065,2)= ".round(1.065,2). "n ";
  65. echo "round(1.075,2)= ".round(1.075,2). "n ";
  66. echo "round(1.085,2)= ".round(1.085,2). "n ";
  67. echo "round(1.095,2)= ".round(1.095,2). "n ";
  68. ?>