Add zeros to a float after the decimal point in Python(Python中小数点后的浮点数加零)
问题描述
我正在从文件中读取数据,对其进行修改并将其写入另一个文件.新文件将被另一个程序读取,因此保留准确的格式至关重要
I am reading in data from a file, modify it and write it to another file. The new file will be read by another program and therefore it is crucial to carry over the exact formatting
例如,我的输入文件中的一个数字是:
for example, one of the numbers on my input file is:
1.000000
我的脚本对列应用了一些数学运算并应该返回
my script applies some math to the columns and should return
2.000000
但是当前返回的是
2.0
如何将浮点数(例如 my_float = 2.0,作为 my_float = 2.00000)写入文件?
How would I write a float for example my_float = 2.0, as my_float = 2.00000 to a file?
推荐答案
格式化到小数点后6位:
Format it to 6 decimal places:
format(value, '.6f')
演示:
>>> format(2.0, '.6f')
'2.000000'
format() 函数按照给定的格式化说明将值转换为字符串.
The format() function turns values to strings following the formatting instructions given.
这篇关于Python中小数点后的浮点数加零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python中小数点后的浮点数加零
基础教程推荐
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
