Python 如何对浮点数进行四舍五入

Python 如何对浮点数进行四舍五入

在本文中,我们将展示如何在Python中对浮点数进行四舍五入。以下是完成此任务的各种方法:

  • 使用round()函数

  • 使用format()函数

  • 使用Decimal模块

  • 使用ceil()函数

使用round()函数

round()函数返回指定小数位数的浮点数,该浮点数是指定数的四舍五入版本。

默认情况下,函数返回最接近的整数,因为小数位数的默认值为0。

语法

round(number, digits)

参数

  • number(必需) - 需要四舍五入的数值

  • digits(可选) - 要四舍五入的小数位数。默认为0

步骤

以下是执行所需任务的算法/步骤:

  • 将输入的 number、digits 值(要四舍五入到的小数位数)作为参数传递到 round() 函数中,以将输入数值四舍五入到指定的小数位数

示例

下面的程序使用 round() 函数返回输入浮点数的四舍五入值,小数位数由用户指定:

# rounding the number up to 2 decimal places
result_1 = round(4.56782, 2)
print("rounding 4.56782 upto 2 decimal places:", result_1)
# rounding the number upto 3 decimal places
result_2 = round(4.56782, 3)
print("rounding 4.56782 upto 3 decimal places:", result_2)
# rounding the number to nearest integer
result_3 = round(4.56782)
print("rounding 4.56782 to nearest integer:", result_3)
# rounding the number to ten's place
result_4 = round(11.56782, -1)
print("rounding 4.56782 to ten's place:", result_4)

输出

执行后,上述程序将生成以下输出:

rounding 4.56782 upto 2 decimal places: 4.57
rounding 4.56782 upto 3 decimal places: 4.568
rounding 4.56782 to nearest integer: 5
rounding 4.56782 to ten's place: 10.0

使用format()函数

步骤

下面是执行所需任务的算法/步骤:

  • 创建一个变量来存储输入的浮点数。

  • 使用 format() 函数(它返回指定格式的输入值的格式化版本)通过将输入数字、格式(要舍入的小数位数)作为参数传递给它来将数字舍入到给定格式的小数位数。

示例

以下程序使用format()函数返回输入浮点数的舍入值,舍入到指定的小数位数:

# input floating-point number
inputNumber = 4.56782
# rounding the number up to 3 decimal places
print("rounding 4.56782 upto 3 decimal places:", format(inputNumber,".3f"))
# rounding the number upto 3 decimal places
print("rounding 35.6578 upto 1 decimal place:", format(35.6578,".1f"))

输出

执行上述程序后将生成以下输出 –

rounding 4.56782 upto 3 decimal places: 4.568
rounding 35.6578 upto 1 decimal place: 35.7

使用Decimal模块

Python的 decimal 模块有助于提高浮点数的精度。要使用这个decimal模块,我们必须导入它。

decimal.Decimal(floatnumber) - 默认会给出50位数字的十进制值。

这里我们使用 value.quantize(decimal.Decimal(‘0.000’)) 来四舍五入到小数点后3位。

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字导入decimal模块。

  • 创建一个变量来存储输入的浮点数。

  • 使用decimal模块的Decimal()函数将给定的浮点数转换。

  • 使用 value.quantize(decimal.Decimal()) 函数将数字四舍五入到指定的小数位数(这里是小数点后面的3位,所以我们在小数点后面加了3个零)。

  • 打印输入的浮点数。

  • 打印输入数字四舍五入到给定小数位数的值。

示例

以下程序使用decimal模块返回输入浮点数的四舍五入值,保留指定的小数位数:

# importing decimal module
import decimal
# input floating-point number
inputNumber = 35.65782
# Converting the given number to decimal
decimalValue = decimal.Decimal(inputNumber)
# rounding the number upto 3 digits after the decimal point
roundedNumber = decimalValue.quantize(decimal.Decimal('0.000'))
# printing the input floating-point number
print("Input floating-point number: ", inputNumber)
# printing the rounded value of the input number upto 3 decimal places
print("Rounding 35.65782 upto 3 decimal places:", roundedNumber)

输出

执行上述程序后,将生成以下输出:

Input floating-point number: 35.65782
Rounding 35.65782 upto 3 decimal places: 35.658

使用ceil()函数

步骤

以下是实现所需任务的算法/步骤:

  • 使用import关键字导入math模块。

  • 创建一个变量来存储输入的浮点数。

  • 使用ceil()函数(返回一个大于或等于该数的最小整数即向上取整的数),将数值舍入到所需的小数位数并打印结果数值。

If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by
# 10, 100, 1000, 10000 numbers respectively

示例

下面的程序使用 ceil() 函数返回输入浮点数的舍入值,保留指定小数位数 –

# importing math module
import math
# input floating-point number
inputNumber = 4.56782
# rounding the number upto 3 decimal places using ceil() function
# If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by 
# 10, 100, 1000, 10000 numbers respectively
print("Rounding 4.56782 upto 3 decimal places:")
print(math.ceil(inputNumber*1000)/1000)

输出

执行以上程序后,将生成以下输出 –

Rounding 4.56782 upto 3 decimal places:
4.568

结论

在本文中,我们学习了如何使用四种不同的方法来对给定的浮点数进行四舍五入。我们还学习了如何使用ceil函数和一些数学逻辑来保留小数点后n位。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程