<?php
// PHP 取整示例
// 1. 使用 (int) 或 intval() 函数进行取整
// 这种方法会直接去掉小数部分,只保留整数部分(相当于向下取整)
$num = 3.7;
$integer = (int)$num; // 或者使用 intval($num);
echo $integer; // 输出: 3
// 2. 使用 floor() 函数进行向下取整
// floor() 函数返回不大于给定参数的最大整数
$num = -3.2;
$floor_value = floor($num);
echo $floor_value; // 输出: -4
// 3. 使用 ceil() 函数进行向上取整
// ceil() 函数返回不小于给定参数的最小整数
$num = 3.2;
$ceil_value = ceil($num);
echo $ceil_value; // 输出: 4
// 4. 使用 round() 函数进行四舍五入
// round() 函数根据指定的精度对浮点数进行四舍五入
$num = 3.5;
$round_value = round($num);
echo $round_value; // 输出: 4
?>
上一篇:php 时间戳
下一篇:php文件
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站