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

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

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

print "%.9f" % numvar

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

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:

print "%.9f" % numvar

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


回答 0

对于Python <3(例如2.6 [参见注释]或2.7),有两种方法可以做到。

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

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

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

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

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

Python 3.6(于2016年12月正式发布)添加了f字符串文字,请参见此处的更多信息,它扩展了str.format方法(使用花括号来f"{numvar:.9f}"解决原始问题),即,

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

解决了问题。查看@ Or-Duan的答案以获取更多信息,但是这种方法很快

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

# 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.

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

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 f string literal, see more information here, which extends the str.format method (use of curly braces such that f"{numvar:.9f}" solves the original problem), that is,

# 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.


回答 1

Python 3.6

为了清楚起见,您可以使用f字符串格式。它的语法几乎与format方法相同,但是要更好一些。

例:

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

有关新f字符串的更多信息:

这是各种测试方法的执行时间的图表(来自上面的最后链接):

Python 3.6

Just to make it clear, you can use f-string formatting. This has almost the same syntax as the format method, but make it a bit nicer.

Example:

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

More reading about the new f string:

Here is a diagram of the execution times of the various tested methods (from last link above):


回答 2

使用round

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

Using round:

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

回答 3

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

newstring = "%.9f" % numvar

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

newstring = "%.9f" % numvar

回答 4

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

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

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

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

回答 5

要使用9位数字设置精度,请获取:

print "%.9f" % numvar

2位数字的返回精度:

print "%.2f" % numvar 

2位数字的返回精度和浮点转换值:

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

To set precision with 9 digits, get:

print "%.9f" % numvar

Return precision with 2 digits:

print "%.2f" % numvar 

Return precision with 2 digits and float converted value:

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

回答 6

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

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

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

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