I am having difficulty converting a float to string in the following manner:
我在将一个浮点数转换成字符串时遇到以下困难:
20.02 --> 20.02
20.016 --> 20.02
20.0 --> 20
It seems that%g
format is the best for that, but I am getting strange results:
看起来%g格式是最好的,但是我得到了奇怪的结果:
In [30]: "%.2g" % 20.03
Out[30]: '20'
In [31]: "%.2g" % 20.1
Out[31]: '20'
In [32]: "%.2g" % 20.3
Out[32]: '20'
In [33]: "%.2g" % 1.2
Out[33]: '1.2'
In [34]: "%.2g" % 1.0
Out[34]: '1'
In [35]: "%.2g" % 2.0
Out[35]: '2'
In [36]: "%.2g" % 2.2
Out[36]: '2.2'
In [37]: "%.2g" % 2.25
Out[37]: '2.2'
In [38]: "%.2g" % 2.26
Out[38]: '2.3'
In [39]: "%.3g" % 2.26
Out[39]: '2.26'
In [40]: "%.3g" % 2.25
Out[40]: '2.25'
In [41]: "%.3g" % 20.02
Out[41]: '20'
In [42]: "%.3g" % 20.016
Out[42]: '20'
In [43]: "%.20g" % 20.016
Out[43]: '20.015999999999998238'
The only solution I know at the moment is checking whether the number is an int
and applying %d
instead of %f
formatting - this is too complicated, I think.
目前我所知道的唯一解决方案是检查数字是否是int类型并应用%d而不是%f格式——我认为这太复杂了。
Does anybody know why the things above are hapenning? How to do this in a simpler way?
有谁知道为什么上面的东西都是有意义的吗?如何用一种更简单的方法?
Thanks.
谢谢。
6 个解决方案
#1
10
Using %f
format specifier:
使用% f格式说明符:
('%.2f' % (value,)).rstrip('0').rstrip('.')
Using round()
function:
使用圆()函数:
str(round(value)).rstrip('0').rstrip('.')
#2
5
Use round together with %g
-- you want at most 2 digits shown, so round to two digits, then use %g
to print it as short as possible:
与%g一起使用圆角——您最多需要显示2位数字,因此圆角为2位,然后使用%g打印尽可能短:
>>> "%g" % round(20.016, 2)
'20.02'
>>> "%g" % round(20, 2)
'20'
#3
2
I'm not sure what exactly you are finding complicated here -- you're getting exactly the results specified e.g. here. E.g.:
我不确定你在这里找到了什么复杂的东西——你得到了具体的结果,比如这里。例如:
In [32]: "%.2g" % 20.3
Out[32]: '20'
In [33]: "%.2g" % 1.2
Out[33]: '1.2'
In each case, you have requested that 2 digits be shown in all, and that's what is happening (both digits come before the trailing dot in one case, one before and one after in the other case, but that's an obvious consequence of the numbers' respective magnitudes).
在每一种情况下,你都要求显示两个数字,这就是所发生的情况(在一种情况下,两个数字都在尾点之前,在另一种情况下,一个在前,一个在后,但这是数字各自大小的明显结果)。
When you ask for 20 digits, you're shown 20 digits -- most aren't meaningful of course (a double-precision IEEE floating point is good for only about 16 digits of precision), so it's more sensible to ask for fewer. You do know of course that floats are represented in binary, as explained here, right? Use decimal (much slower, of course, since what your computer's hardware supplies is binary floating point, the decimal version must all be synthesized in software) is what you need are decimal-represented floats (e.g., for monetary computations).
当你要求20位数字时,你会看到20位数字——当然大多数都没有意义(双精度的IEEE浮点数只适用于16位左右的精度),所以要求更少的数字更明智。你当然知道浮动是用二进制表示的,就像这里解释的那样,对吧?使用十进制(当然,要慢得多,因为你的计算机硬件提供的是二进制浮点数,十进制的版本必须全部在软件中合成)是你所需要的小数表示的浮点数(例如,货币计算)。
#4
1
Try something a little less than 20 digits. If you use 20 digits, it will give you the 20 digit representation of the floating point value, which might look a bit odd.
尝试一些少于20位数的数字。如果你用20位数字,它会给出浮点值的20位表示,这看起来可能有点奇怪。
#6
1
You can use .format
instead of %
.
您可以使用.format而不是%。
>>> '{:.2f}'.format(3.0101)
'3.01'
>>> type('{:.2f}'.format(3.0101))
str
There is lot more stuff which is not possible using %
but can be done using .format
.
使用%是不可能的,但是可以使用.format完成。
For more formatting details, take a look at pyformat
有关更多格式化细节,请查看pyformat
#1
10
Using %f
format specifier:
使用% f格式说明符:
('%.2f' % (value,)).rstrip('0').rstrip('.')
Using round()
function:
使用圆()函数:
str(round(value)).rstrip('0').rstrip('.')
#2
5
Use round together with %g
-- you want at most 2 digits shown, so round to two digits, then use %g
to print it as short as possible:
与%g一起使用圆角——您最多需要显示2位数字,因此圆角为2位,然后使用%g打印尽可能短:
>>> "%g" % round(20.016, 2)
'20.02'
>>> "%g" % round(20, 2)
'20'
#3
2
I'm not sure what exactly you are finding complicated here -- you're getting exactly the results specified e.g. here. E.g.:
我不确定你在这里找到了什么复杂的东西——你得到了具体的结果,比如这里。例如:
In [32]: "%.2g" % 20.3
Out[32]: '20'
In [33]: "%.2g" % 1.2
Out[33]: '1.2'
In each case, you have requested that 2 digits be shown in all, and that's what is happening (both digits come before the trailing dot in one case, one before and one after in the other case, but that's an obvious consequence of the numbers' respective magnitudes).
在每一种情况下,你都要求显示两个数字,这就是所发生的情况(在一种情况下,两个数字都在尾点之前,在另一种情况下,一个在前,一个在后,但这是数字各自大小的明显结果)。
When you ask for 20 digits, you're shown 20 digits -- most aren't meaningful of course (a double-precision IEEE floating point is good for only about 16 digits of precision), so it's more sensible to ask for fewer. You do know of course that floats are represented in binary, as explained here, right? Use decimal (much slower, of course, since what your computer's hardware supplies is binary floating point, the decimal version must all be synthesized in software) is what you need are decimal-represented floats (e.g., for monetary computations).
当你要求20位数字时,你会看到20位数字——当然大多数都没有意义(双精度的IEEE浮点数只适用于16位左右的精度),所以要求更少的数字更明智。你当然知道浮动是用二进制表示的,就像这里解释的那样,对吧?使用十进制(当然,要慢得多,因为你的计算机硬件提供的是二进制浮点数,十进制的版本必须全部在软件中合成)是你所需要的小数表示的浮点数(例如,货币计算)。
#4
1
Try something a little less than 20 digits. If you use 20 digits, it will give you the 20 digit representation of the floating point value, which might look a bit odd.
尝试一些少于20位数的数字。如果你用20位数字,它会给出浮点值的20位表示,这看起来可能有点奇怪。
#5
#6
1
You can use .format
instead of %
.
您可以使用.format而不是%。
>>> '{:.2f}'.format(3.0101)
'3.01'
>>> type('{:.2f}'.format(3.0101))
str
There is lot more stuff which is not possible using %
but can be done using .format
.
使用%是不可能的,但是可以使用.format完成。
For more formatting details, take a look at pyformat
有关更多格式化细节,请查看pyformat