在 Python 中的 Decimal 之后向 Float 添加零
使用格式化字符串文字将零添加到小数点后的浮点数。
格式化字符串文字可以使用格式规范迷你语言将 N 个零添加到小数点后的浮点数。
my_float = 3.0
result = f'{my_float:.3f}'
print(result) # 👉️ '3.000'
格式化字符串文字 (f-strings) 让我们通过在字符串前加上 f
来在字符串中包含表达式。
my_str = 'is subscribed:'
my_bool = True
result = f'{my_str} {my_bool}'
print(result) # 👉️ is subscribed: True
确保将表达式用大括号括起来 - {expression}
。
我们还能够在 f
字符串的表达式中使用格式规范迷你语言。
my_float = 3.2
result_1 = f'{my_float:.3f}'
print(result_1) # 👉️ '3.200'
result_2 = f'{my_float:.5f}'
print(result_2) # 👉️ '3.20000'
result_3 = f'{my_float:.6f}'
print(result_3) # 👉️ '3.200000'
句点后的数字用于确定浮点数应有多少位小数。
我们还可以在句点后明确指定填充字符 (0)。
my_float = 3.2
result_1 = f'{my_float:.03f}'
print(result_1) # 👉️ '3.200'
result_2 = f'{my_float:.05f}'
print(result_2) # 👉️ '3.20000'
result_3 = f'{my_float:.06f}'
print(result_3) # 👉️ '3.200000'
当没有在宽度字段前面给出一个零的显式对齐时,我们可以在小数点后用零填充数字。
如果小数点后浮点数的宽度存储在变量中,我们也可以使用此方法。
my_float = 3.2
width_after_decimal = 4
result = f'{my_float:.{width_after_decimal}f}'
print(result) # 👉️ '3.2000'
请注意
,我们在表达式中使用了第二组大括号来计算变量。
格式化字符串文字方法将始终返回一个字符串值。
没有办法在小数点后用零填充浮点数并让它保持为数字,因为 Python 不会保留无关紧要的尾随零。
my_float = 3.00000000
print(my_float) # 👉️ 3.0
使用 str.format()
向小数点后的浮点数添加零
我们还可以使用 str.format()
方法将零添加到小数点后的浮点数。
my_float = 3.0
result = '{:.3f}'.format(my_float)
print(result) # '3.000'
result = '{:.4f}'.format(my_float)
print(result) # '3.0000'
result = '{:.6f}'.format(my_float)
print(result) # '3.000000'
str.format
方法执行字符串格式化操作。
first = 'fql'
last = 'jiyik'
result = "Name: {} {}".format(first, last)
print(result) # 👉️ "Name: fql jiyik"
调用该方法的字符串可以包含使用花括号 {}
指定的替换字段。
我们使用格式规范迷你语言,就像我们使用格式化字符串文字时所做的那样。
使用 format() 向小数点后的浮点数添加零
或者,我们可以使用 format()
函数。
my_float = 3.0
result = format(my_float, '.3f')
print(result) # 👉️ '3.000'
print(type(result)) # 👉️ <class 'str'>
我们使用格式函数将零添加到小数点后的浮点数。
该函数采用一个值和一个格式说明符,并根据提供的格式说明符将该值转换为格式化表示。
格式说明符中的 f
类型代表定点表示法。
my_float = 3.0
result_1 = format(my_float, '.3f')
print(result_1) # '3.000'
result_2 = format(my_float, '.4f')
print(result_2) # '3.0000'
result_3 = format(my_float, '.6f')
print(result_3) # '3.000000'
请注意
,format()
函数返回的值是一个字符串。
这是必要的,因为 Python 不会保留任何不重要的尾随零。
my_float = 3.00000000
print(my_float) # 👉️ 3.0
当小数点后的浮点宽度存储在变量中时,也可以使用这种方法。
my_float = 3.0
width_after_decimal = 3
result_1 = format(my_float, f'.{width_after_decimal}f')
print(result_1) # '3.000'
相关文章
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 日期时间。