您的位置:首页 > 编程语言 > PHP开发

PHP关于数据的舍入关键字归类:round() ceil() floor() number_format()

2013-03-11 16:13 288 查看
round -- 对浮点数进行四舍五入

例子:
<?php
echo round(3.4);         // 3
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.055, 2);    // 5.06
?>

ceil -- 进一法取整
例子:
<?php
echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
?>


floor -- 舍去法取整,跟ceil刚刚相反

例子:
[code]<?php
echo floor(4.3);   // 4
echo floor(9.999); // 9
?>


[/code]
number_format -- 格式化数字为千分位
<?php

$number = 1234;

// english notation (default)

$english_format_number = number_format($number);

echo "First:".$english_format_number."<br/>";

// French notation

$nombre_format_francais = number_format($number, 2, ',', '');

echo "Second:".$nombre_format_francais."<br/>";

// French notation

$number = 1234.564;

$nombre_format_francais = number_format($number, 2, ',', '');

echo "Third:".$nombre_format_francais."<br/>";

$number = 1234.5678;

// english notation without thousands seperator

$english_format_number = number_format($number, 2, '.', '');

echo "Forth:".$english_format_number."<br/>";

?>

显示结果最终如下:

First:1,234

Second:1234,00

Third:1234,56

Forth:1234.57
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: