如何使用Python将浮点数舍入到2位小数?
在本文中,我们将展示如何使用Python将浮点数舍入至 2 位小数。以下是实现此任务的各种方法:
- 使用round()函数
-
使用format()函数
-
使用Decimal模块
-
使用ceil()函数
使用round()函数
round()函数返回一个具有指定小数位数的浮点数,该浮点数是指定数字的四舍五入版本。
由于小数位数的默认值为0,所以该函数将返回最近的整数。
语法
round(number, digits)
参数
- number(必填) - 需要四舍五入的数字。
-
digits(可选) - 要保留的小数位数,默认为0。
算法(步骤)
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储输入的数字。
-
将输入的数字和小数值2作为参数传递给round()函数,以使输入的数字保留两位小数。
-
打印输入浮点数的四舍五入值,保留 2 位小数。
示例
以下程序使用round()函数返回输入浮点数的四舍五入值,保留2位小数。
# input floating-point number
inputNumber = 3.367824
# rounding the number up to 2 decimal places
roundedNumber = round(inputNumber, 2)
# print the rounded value of floating-point number up to 2 decimals
print("Rounding 3.367824 upto 2 decimal places:", roundedNumber)
输出
在执行后,上述程序将生成以下输出:
Rounding 3.367824 upto 2 decimal places: 3.37
使用format()函数
算法(步骤)
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储输入的浮点数。
-
使用format()函数(它返回指定格式的输入值的格式化版本)通过将输入数字、格式(保留2位小数)作为参数传递给它,将数字四舍五入到2位小数。
示例
以下程序使用format()函数返回输入的浮点数四舍五入到2位小数的值:
# input floating-point number
inputNumber = 4.56782
# rounding the number upto 2 decimal places
print("Rounding 4.56782 upto 2 decimal places:", format(inputNumber,".2f"))
输出
执行上述程序后,将生成以下输出:
Rounding 4.56782 upto 2 decimal places: 4.57
使用 Decimal 模块
Python的 decimal 模块 有助于提高浮点数的精度。如果要使用这个 decimal 模块,必须先导入它。
decimal.Decimal(floatnumber) − 默认给出一个有50位小数位的十进制数值。
在这里,我们使用 value.quantize(decimal.Decimal(‘0.00’)) 将数字四舍五入到小数点后 2 位。
算法(步骤)
执行所需任务的算法/步骤如下:
- 使用 import 关键字导入 decimal 模块。
-
创建一个变量来存储输入的浮点数。
-
使用 decimal 模块的 Decimal() 函数将给定的浮点数转换为十进制数。
-
使用 value.quantize(decimal.Decimal()) 函数将数字四舍五入到2位小数点(这里是两个0表示两位小数点)。
-
打印输入的浮点数。
-
打印输入数字四舍五入到2位小数点后的值。
示例
以下程序使用decimal模块返回输入浮点数四舍五入到2位小数的值-
# 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 2 digits after the decimal point
roundedNumber = decimalValue.quantize(decimal.Decimal('0.00'))
# printing the input floating-point number
print("Input floating-point number: ", inputNumber)
# printing the rounded value of the input number upto 2 decimal places
print("Rounding 35.65782 upto 2 decimal places:", roundedNumber)
输出
执行上述程序后,将生成以下输出:
Input floating-point number: 35.65782
Rounding 35.65782 upto 2 decimal places: 35.66
使用 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 2 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 2 decimal places:")
print(math.ceil(inputNumber*100)/100)
输出
执行上述程序后,将会生成以下输出:
Input floating-point number: 35.65782
Rounding 4.56782 upto 2 decimal places:
4.57
结论
在本文中,我们学习了如何使用四种不同的方法将给定的浮点数四舍五入到小数点后两位。我们使用了ceil函数和一些数学逻辑,学习了如何四舍五入到小数点后两位。我们还学习了如何使用decimal模块将给定的浮点数转换成一个十进制数,并量化它。