在 Python 中将浮点数舍入为 1、2 或 3 位小数
使用 round()
函数将浮点数舍入到小数点后 1、2 或 3 位,例如 result = round(6.36789, 2)
。
round()
函数会将浮点数四舍五入到指定的小数位数并返回结果。
my_float = 6.36789
# ✅ 将浮点数四舍五入到小数点后 1、2 或 3 位 (round())
result = round(my_float, 1)
print(result) # 👉️ 6.4
result = round(my_float, 2)
print(result) # 👉️ 6.37
result = round(my_float, 3)
print(result) # 👉️ 6.368
该示例使用 round()
函数将浮点数四舍五入到指定的小数位数。
my_float = 6.36789
result = round(my_float, 3)
print(result) # 👉️ 6.368
round
函数采用以下 2 个参数:
- number 要四舍五入到小数点后 ndigits 精度的数字
- ndigits 小数点后的位数,运算后的数字应该有(可选)
round
函数返回四舍五入到小数点后 ndigits 精度的数字。
如果我们需要在浮点数列表上调用 round()
函数,请使用列表推导。
list_of_floats = [1.42342365, 3.438834, 5.5843854]
result = [round(num, 2) for num in list_of_floats]
print(result) # 👉️ [1.42, 3.44, 5.58]
我们使用列表理解来迭代浮点数列表,并将每个数字传递给 round()
函数。
使用 f-string
将浮点数舍入到小数点后 1、2 或 3 位
或者,我们可以使用格式化的字符串文字。
my_float = 6.36789
result = f'{my_float:.1f}'
print(result) # 👉️ '6.4'
result = f'{my_float:.2f}'
print(result) # 👉️ '6.37'
result = f'{my_float:.3f}'
print(result) # 👉️ '6.368'
number_of_decimals = 3
result = f'{my_float:.{number_of_decimals}f}'
print(result) # 👉️ '6.368'
格式化字符串文字 f-strings
让我们通过在字符串前加上 f
来在字符串中包含表达式。
确保将表达式用大括号括起来 - {expression}
。
格式化字符串文字还使我们能够在表达式块中使用格式特定的迷你语言。
my_float = 6.36789
# 👇️ 四舍五入到小数点后一位: 6.4
print(f'Rounded to 2 decimals: {my_float:.1f}')
# 👇️ 四舍五入到小数点后二位: 6.37
print(f'Rounded to 2 decimals: {my_float:.2f}')
# 👇️ 四舍五入到小数点后三位: 6.368
print(f'Rounded to 3 decimals: {my_float:.3f}')
句点后的数字是浮点数应具有的小数位数。
如果我们将小数位数存储在变量中,请将其用大括号括在 f 字符串中。
my_float = 6.36789
number_of_decimals = 3
result = f'{my_float:.{number_of_decimals}f}'
print(result) # 👉️ '6.368'
如果我们需要将浮点数列表四舍五入到小数点后 N 位,请使用列表推导。
my_float = 6.36789
list_of_floats = [2.298438438, 4.5848548, 8.8347347]
result = [f'{item:.3f}' for item in list_of_floats]
print(result) # 👉️ ['2.298', '4.585', '8.835']
我们使用列表理解来迭代浮点数列表。
列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。
在每次迭代中,我们使用格式化字符串文字将当前浮点数四舍五入到小数点后 3 位并返回结果。
在 Python 中将浮点数向下舍入为 1、2 或 3 位小数
将浮点数舍入为 N 位小数是一个三步过程:
-
调用
math.floor()
方法,将乘以 100 的数字传递给它。 - 将结果除以 100。
- 计算结果将是四舍五入到 2 位小数的浮点数。
import math
def round_down_float_to_2_decimals(num):
return math.floor(num * 100) / 100
print(round_down_float_to_2_decimals(4.6789)) # 👉️ 4.67
可以使用相同的方法将浮点数向下舍入到小数点后 3 位。
import math
def round_down_float_to_3_decimals(num):
return math.floor(num * 1000) / 1000
print(round_down_float_to_3_decimals(4.6789)) # 👉️ 4.678
可以使用相同的方法将浮点数向下舍入到小数点后一位。
import math
def round_down_float_to_1_decimal(num):
return math.floor(num * 10) / 10
print(round_down_float_to_1_decimal(2.6789)) # 👉️ 2.6
math.floor
方法返回小于或等于提供的数字的最大整数。
import math
print(math.floor(15.999)) # 👉️ 15
print(math.floor(15.001)) # 👉️ 15
如果传入的数字有小数部分,
math.floor
方法会将数字向下舍入。
下面是一个将浮点数舍入到小数点后两位的分步示例。
import math
print(4.4567 * 100) # 👉️ 445.66999999999996
print(4.1234 * 100) # 👉️ 412.34000000000003
print(math.floor(4.4567 * 100)) # 👉️ 445
print(math.floor(4.1234 * 100)) # 👉️ 412
print(math.floor(4.4567 * 100) / 100) # 👉️ 4.45
print(math.floor(4.1234 * 100) / 100) # 👉️ 4.12
我们先将数字乘以 100,然后除以 100,将小数点左右移动 2 位,这样 math.floor() 就可以计算百位了。
这是一个两步过程:
- 将数字乘以 100,并将结果向下舍入到最接近的整数。
- 将结果除以 100,将浮点数向下舍入到小数点后两位。
在 Python 中将浮点数向上舍入为 1、2 或 3 位小数
将浮点数四舍五入为 N 位小数是一个三步过程:
-
调用
math.ceil()
方法,将乘以 100 的数字传递给它。 - 将结果除以 100。
- 计算结果将是四舍五入到 2 位小数的浮点数。
import math
def round_up_float_to_2_decimals(num):
return math.ceil(num * 100) / 100
print(round_up_float_to_2_decimals(4.6123)) # 👉️ 4.62
可以使用相同的方法将浮点数四舍五入到小数点后 3 位。
import math
def round_up_float_to_3_decimals(num):
return math.ceil(num * 1000) / 1000
print(round_up_float_to_3_decimals(4.6123)) # 👉️ 4.62
可以使用相同的方法将浮点数四舍五入到小数点后一位。
import math
def round_up_float_to_1_decimal(num):
return math.ceil(num * 10) / 10
print(round_up_float_to_1_decimal(2.6123)) # 👉️ 2.7
math.ceil
方法返回大于或等于提供的数字的最小整数。
import math
print(math.ceil(76.001)) # 👉️ 77
print(math.ceil(76.999)) # 👉️ 77
如果传入的数字有小数部分,math.ceil
方法会将数字向上舍入。
下面是一个将浮点数四舍五入到小数点后两位的分步示例。
import math
print(4.4567 * 100) # 👉️ 445.66999999999996
print(4.1234 * 100) # 👉️ 412.34000000000003
print(math.ceil(4.4567 * 100)) # 👉️ 446
print(math.ceil(4.1234 * 100)) # 👉️ 413
print(math.ceil(4.4567 * 100) / 100) # 👉️ 4.46
print(math.ceil(4.1234 * 100) / 100) # 👉️ 4.13
我们先将数字乘以 100,然后除以 100,将小数点左右移动 2 位,这样 math.ceil() 就可以计算百位了。 这是一个两步过程:
- 将数字乘以 100,并将结果四舍五入到最接近的整数。
- 将结果除以 100,将浮点数四舍五入到小数点后 2 位。
在 Python 中将浮点数列表四舍五入到小数点后两位
将浮点数列表四舍五入到小数点后两位:
- 使用列表理解来遍历列表。
-
使用
round()
函数将每个浮点数四舍五入到小数点后两位。 - 新列表将包含四舍五入到两位小数的浮点数。
list_of_floats = [2.4834843583, 4.28384291, 8.47989238]
# ✅ 将浮点数舍入到小数点后两位 (round())
result = [round(item, 2) for item in list_of_floats]
print(result) # 👉️ [2.48, 4.28, 8.48]
# ------------------------------------------
# ✅ 将浮点数列表格式化为小数点后两位(f-string)
result = [f'{item:.2f}' for item in list_of_floats]
print(result) # 👉️ ['2.48', '4.28', '8.48']
# ------------------------------------------
# ✅ 将浮点数舍入到小数点后两位 (numpy.around())
import numpy as np
result = list(np.around(list_of_floats, 2))
print(result) # 👉️ [2.48, 4.28, 8.48]
我们使用列表推导来迭代浮点数列表。
list_of_floats = [2.4834843583, 4.28384291, 8.47989238]
result = [round(item, 2) for item in list_of_floats]
print(result) # 👉️ [2.48, 4.28, 8.48]
列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。
在每次迭代中,我们使用 round()
函数将当前浮点数四舍五入到小数点后两位。
round
函数采用以下 2 个参数:
-
number 要四舍五入到小数点后
ndigits
精度的数字 - ndigits 小数点后的位数,运算后的数字应该有(可选)
round
函数返回四舍五入到小数点后 ndigits 精度的数字。
或者,我们可以使用格式化的字符串文字。
使用 f-string 将浮点数列表舍入到小数点后两位
这是一个三步过程:
- 使用列表推导来遍历列表。
- 使用格式化字符串文字将每个浮点数四舍五入到小数点后两位。
- 新列表将包含四舍五入到两位小数的浮点数。
list_of_floats = [2.4834843583, 4.28384291, 8.47989238]
result = [f'{item:.2f}' for item in list_of_floats]
print(result) # 👉️ ['2.48', '4.28', '8.48']
格式化字符串文字
(f-strings)
让我们通过在字符串前加上f
来在字符串中包含表达式。
确保将表达式用大括号括起来 - {expression}
。
格式化字符串文字还使我们能够在表达式块中使用格式特定的迷你语言。
my_float = 2.4834843583
print(f'{my_float:.2f}') # 👉️ 2.48
print(f'{my_float:.3f}') # 👉️ 2.483
句点后的数字是浮点数应具有的小数位数。
如果我们将小数位数存储在变量中,请将其用大括号括在 f
字符串中。
list_of_floats = [2.4834843583, 4.28384291, 8.47989238]
number_of_decimals = 2
result = [f'{item:.{number_of_decimals}f}' for item in list_of_floats]
print(result) # 👉️ ['2.48', '4.28', '8.48']
如果你使用 NumPy,你可以使用 numpy.around()
方法。
使用 numpy.around() 将浮点数列表舍入到小数点后两位
这是一个两步过程:
-
使用
numpy.around()
方法将列表中的每个浮点数四舍五入到小数点后两位。 -
使用
list()
类将 NumPy 数组转换为列表。
import numpy as np
list_of_floats = [2.4834843583, 4.28384291, 8.47989238]
result = list(np.around(list_of_floats, 2))
print(result) # 👉️ [2.48, 4.28, 8.48]
确保安装了 numpy
模块以便能够运行代码示例:
$ pip install numpy
$ pip3 install numpy
numpy.around
方法采用类似数组的对象并将其项目四舍五入到给定的小数位数。
decimals 参数默认为 0。
最后一步是使用 list()
类将 numpy 数组转换为列表。
列表类接受一个可迭代对象并返回一个列表对象。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。