Python中小数点后的浮点数加零
- 作者: uoykcuf
- 来源: 51数据库
- 2022-10-21
问题描述
我正在从文件中读取数据,对其进行修改并将其写入另一个文件.新文件将被另一个程序读取,因此保留准确的格式至关重要
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.
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
