Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python round()

作者:冫LOVE灬丶棒棒   发布日期:2025-11-09   浏览:98

# 示例代码:使用 round() 函数

# round() 函数用于对浮点数进行四舍五入
# 基本语法:round(number, ndigits)

# 没有指定小数位数时,返回最接近的整数
num1 = 3.7
rounded_num1 = round(num1)
print(f"round({num1}) = {rounded_num1}")  # 输出: round(3.7) = 4

# 指定小数位数时,返回指定精度的四舍五入结果
num2 = 3.1415926
rounded_num2 = round(num2, 2)
print(f"round({num2}, 2) = {rounded_num2}")  # 输出: round(3.1415926, 2) = 3.14

# 对于 .5 的情况,round() 函数会向最近的偶数舍入
num3 = 2.5
rounded_num3 = round(num3)
print(f"round({num3}) = {rounded_num3}")  # 输出: round(2.5) = 2

num4 = 3.5
rounded_num4 = round(num4)
print(f"round({num4}) = {rounded_num4}")  # 输出: round(3.5) = 4

上一篇:python代码运行器

下一篇:python读取ini文件

大家都在看

python时间格式

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站