round

(PHP 4, PHP 5, PHP 7)

round�Ը�����������������

˵��

round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ) : float

���ؽ� val ����ָ������ precision��ʮ����С��������ֵ���Ŀ��������������Ľ����precision Ҳ�����Ǹ������㣨Ĭ��ֵ����

Note: PHP Ĭ�ϲ�����ȷ�������� "12,300.2" ���ַ��������ַ���ת��Ϊ��ֵ��

����

val

Ҫ�����ֵ

precision

��ѡ��ʮ����С��������ֵ���Ŀ��

mode

����֮һ�� PHP_ROUND_HALF_UP�� PHP_ROUND_HALF_DOWN PHP_ROUND_HALF_EVEN �� PHP_ROUND_HALF_ODD

����ֵ

����������ֵ

����

Example #1 round() ����

<?php
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.60);      // 4
echo round(1.955832);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.0452);    // 5.05
echo round(5.0552);    // 5.06
?>

Example #2 mode ����

<?php
echo round(9.50PHP_ROUND_HALF_UP);   // 10
echo round(9.50PHP_ROUND_HALF_DOWN); // 9
echo round(9.50PHP_ROUND_HALF_EVEN); // 10
echo round(9.50PHP_ROUND_HALF_ODD);  // 9

echo round(8.50PHP_ROUND_HALF_UP);   // 9
echo round(8.50PHP_ROUND_HALF_DOWN); // 8
echo round(8.50PHP_ROUND_HALF_EVEN); // 8
echo round(8.50PHP_ROUND_HALF_ODD);  // 9
?>

������־

�汾 ˵��
5.3.0 ������ mode ����
5.2.7 round() ���ڲ������޸ķ��� C99 �ı�׼��

�μ�

  • ceil() - ��һ��ȡ��
  • floor() - ��ȥ��ȡ��
  • number_format() - ��ǧλ�ָ�����ʽ��ʽ��һ������