简体   繁体   English

Python浮点字符串格式无法正常工作

[英]Python Float String Formatting Not Working Right

I'm pretty sure that I'm doing this correctly but not getting the results that I expect from Python 2.7.3 string formatting mini-language. 我很确定自己可以正确执行此操作,但是没有从Python 2.7.3字符串格式化迷你语言中获得期望的结果。 I'm trying to format the output of a float to 3 integer values and a varying number of decimal values. 我正在尝试将float的输出格式化为3个整数值和不同数量的十进制值。 Everything works except the integer values. 除整数值外,一切正常。

By itself, the following code works to get me 3 integer values... 就其本身而言,以下代码可为我提供3个整数值...

num = 3
value = '{:03}'.format(num)

returns '003'

And floats work... 和浮动工作...

num = 3.12345
value = '{:01.2f}'.format(num)

returns '3.12'

However, combining the two does not seem to work properly. 但是,将两者结合似乎无法正常工作。 The decimal precision works, but the integer precision does not... 十进制精度有效,但整数精度无效...

num = '3.12345'
value = '{:03.2f}'.format(num)

returns '3.12'

instead of the expected '003.12'

The same is true if I try any of the following formats... 如果我尝试以下任何一种格式,情况也是如此...

value = '{:03.02f}'.format(num)
- or -
value = '{0:3.2f}'.format(num)
- or -
value = '{:3.02f}'.format(num)
- or -
value = '{0:3.02f}'.format(num)
- or -
value = '{0:03.2f}'.format(num)
- or -
value = '{0:03.02f}'.format(num)
- or -
value = '{:0>3.2f}'.format(num)
etc...

Which all return the same '3.12' instead of '003.12'

If you're curious about what I'm doing for the varying / dynamic decimal precision, the point is to keep all the values the same length, but different sets of values may not have the same precision. 如果您对我为可变/动态十进制精度所做的事情感到好奇,那么重点是保持所有值的长度相同,但是不同的值集可能不具有相同的精度。 So I take the smaller of the longest precision value or 10 and force the others to match like this.. 因此,我采用最长精度值中的较小者或10,然后强制其他精度匹配。

from decimal import Decimal

dVals = [
    abs(Decimal(val1).as_tuple().exponent), # Get the number of decimal points
    abs(Decimal(val2).as_tuple().exponent), # "" ""
    abs(Decimal(val3).as_tuple().exponent), # "" ""
    abs(Decimal(val4).as_tuple().exponent), # "" ""
    ]

    p = max(dVals)  # Get the largest value
    p = min(p, 10)  # Get the smaller of the largest value or 10

vals = [val1, val2, val3, val4]

for val in vals:
    value = '{:0>3.{prec}f}'.format(val, prec = p)
    # do whatever with value here...

Again, this all works perfectly, except that the integer value never returns with 3 precision places when I combine it with float precision. 再次,这一切都很好,除了当我将整数值与float精度结合使用时,整数值永远不会以3个精度位返回。 So all my efforts to ensure the values output with the same formatted length are all for naught. 因此,我为确保输出具有相同格式长度的值所做的所有努力都是徒劳的。 Ultimately, the output should look similar to... 最终,输出应类似于...

'009.123456700'
'180.101010101'
'054.987654321'

Instead, it looks like this... 相反,它看起来像这样...

'9.123456700'
'180.101010101'
'54.987654321'

In other words, ugly. 换句话说,丑陋。 :-| :-|

At the very least, I would accept the following output... 至少,我会接受以下输出...

'  9.123456700'
'180.101010101'
' 54.987654321'

Any ideas what I'm doing wrong here? 有任何想法我在这里做错了吗? Your help is much appreciated! 非常感谢您的帮助!

Regards, 问候,

-RMWChaos -RMWChaos

The first digit is the total width of the result, not the width before the decimal point. 第一位数字是结果的总宽度,而不是小数点前的宽度。

>>> '{:06.2f}'.format(3.12345)
'003.12'
num = '3.12345'
value = '{:03.2f}'.format(num)

Here 03 takes into account all digits (including the floating point itself). 这里03考虑了所有数字(包括浮点本身)。 so you should do something like this: 因此,您应该执行以下操作:

value = '{:06.2f}'.format(num)

this will get you: '003.12' 这将使您: '003.12'

count everything in this string '003.12' , the result is 6. But you were using 3 which is even less than the count of the minimal representation, which is why you got '3.12' 计算该字符串'003.12' ,结果为6。但是您使用的3甚至比最小表示的计数还要少,这就是为什么得到'3.12'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM