如何将浮点数转换为字符串,但转换为小数点后第六位 - Python

发布于 2024-10-10 07:18:02 字数 63 浏览 0 评论 0原文

我需要将浮点数转换为字符串,但转换为小数点后第六位。我如何执行这样的任务而不将其转换为带有 E 表示法的字符串?

I need to convert a float to a string, but to the sixth decimal. How would I preform a task like that without converting it into a string with an E notation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里泪两行 2024-10-17 07:18:02
>>> "{0:.6f}".format(123455.12345678)
'123455.123457'

还要注意舍入。

请参阅有关 格式字符串语法格式规范迷你语言

>>> "{0:.6f}".format(123455.12345678)
'123455.123457'

Note the rounding as well.

See the python docs on format string syntax and format specification mini-language.

黯淡〆 2024-10-17 07:18:02

如果您使用旧版本的 python:

print "%.6f" % 1.23456789

如果您想要“e 表示法”,您可以尝试

print "%.6e" % 1.23456789

print "%.6E" % 1.23456789

虽然我现在不确定此方法是否具有很大的灵活性。

If you're on an older version of python:

print "%.6f" % 1.23456789

If you want the "e notation" you can try

print "%.6e" % 1.23456789

or

print "%.6E" % 1.23456789

Though I'm not sure now much flexibility you have with this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文