Python 将浮点数转换为一定精度,然后复制到字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15263597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:38:59  来源:igfitidea点击:

Convert floating point number to a certain precision, and then copy to string

pythonstringfloating-point

提问by pauliwago

I have a floating point number, say 135.12345678910. I want to concatenate that value to a string, but only want 135.123456789. With print, I can easily do this by doing something like:

我有一个浮点数,比如说135.12345678910。我想将该值连接到一个字符串,但只想要135.123456789. 通过打印,我可以通过执行以下操作轻松做到这一点:

print "%.9f" % numvar

with numvarbeing my original number. Is there an easy way to do this?

numvar是我原来的号码。是否有捷径可寻?

采纳答案by HAL 9001

With Python < 3 (e.g. 2.6 [see comments] or 2.7), there are two ways to do so.

对于 Python < 3(例如 2.6 [见评论] 或 2.7),有两种方法可以做到这一点。

# Option one
older_method_string = "%.9f" % numvar

# Option two
newer_method_string = "{:.9f}".format(numvar)

But note that for Python versions above 3 (e.g. 3.2 or 3.3), option two is preferred.

但请注意,对于 3 以上的 Python 版本(例如 3.2 或 3.3),首选选项二。

For more information on option two, I suggest this link on string formatting from the Python documentation.

有关选项二的更多信息,我建议使用 Python 文档中有关字符串格式的链接

And for more information on option one, this link will suffice and has info on the various flags.

有关选项一的更多信息,此链接就足够了,并且包含有关各种标志的信息

Python 3.6 (officially released in December of 2016), added the fstring literal, see more information here, which extends the str.formatmethod (use of curly braces such that f"{numvar:.9f}"solves the original problem), that is,

Python 3.6(2016年12月正式发布),增加了f字符串字面量,更多信息看这里,扩展了str.format方法(使用大括号f"{numvar:.9f}"解决了原来的问题),即

# Option 3 (versions 3.6 and higher)
newest_method_string = f"{numvar:.9f}"

solves the problem. Check out @Or-Duan's answer for more info, but this method is fast.

解决了这个问题。查看@Or-Duan's answer了解更多信息,但这种方法很快

回答by John La Rooy

It's not print that does the formatting, It's a property of strings, so you can just use

进行格式化的不是打印,而是字符串的属性,因此您可以使用

newstring = "%.9f" % numvar

回答by shantanoo

Using round:

使用round

>>> numvar = 135.12345678910
>>> str(round(numvar, 9))
'135.123456789'

回答by Yu Hao

In case the precision is not known until runtime, this other formatting option is useful:

如果直到运行时才知道精度,这个其他格式选项很有用:

>>> n = 9
>>> '%.*f' % (n, numvar)
'135.123456789'

回答by ethemsulan

The strfunction has a bug. Please try the following. You will see '0,196553' but the right output is '0,196554'. Because the strfunction's default value is ROUND_HALF_UP.

str功能有一个错误。请尝试以下操作。您将看到“0,196553”,但正确的输出是“0,196554”。因为str函数的默认值是 ROUND_HALF_UP。

>>> value=0.196553500000 
>>> str("%f" % value).replace(".", ",")

回答by Or Duan

Python 3.6

蟒蛇 3.6

Just to make it clear, you can use f-stringformatting. This has almost the same syntax as the formatmethod, but make it a bit nicer.

为了清楚起见,您可以使用f-string格式。这与format方法的语法几乎相同,但使它更好一点。

Example:

例子:

print(f'{numvar:.9f}')

More reading about the new f string:

更多关于新的 f 字符串的阅读:

Enter image description here

在此处输入图片说明

回答by Tejas Tank

To set precision with 9 digits, get:

要设置 9 位精度,请获取:

print "%.9f" % numvar

Return precision with 2 digits:

返回精度为 2 位:

print "%.2f" % numvar 

Return precision with 2 digits and float converted value:

返回精度为 2 位和浮点转换值:

numvar = 4.2345
print float("%.2f" % numvar)