简体   繁体   English

小数点后加零

[英]Adding zeros after the decimal point

I have a certain dictionary key and its values that I wish to print out in a string in a certain fashion. 我有一个特定的字典键及其值,希望以某种方式以字符串形式打印出来。 The key and its values is: 密钥及其值是:

 'HUF': (315.89, 307.09)

and the desired output is: 所需的输出是:

HUF....315.8900....307.0900

So far, I have used the following code: 到目前为止,我已经使用以下代码:

'{i:.<8}{e[0]:.<3f}{e[1]:.>12}'.format(i=i, e=e)

in which "i" represents the key and "e[0]" and "e[1]" represent the values. 其中“ i”代表键,“ e [0]”和“ e [1]”代表值。 But when I run this code the output is: 但是,当我运行此代码时,输​​出为:

HUF.....315.890000......307.09

Is there any way to to print the first number without the two added zeros? 有没有办法打印不带两个加零的第一个数字?

Given: 鉴于:

>>> i='HUF'
>>> e=(315.89, 307.09)

Try: 尝试:

>>> '{i:.<8}{e[0]:.<.04f}{e[1]:.>12.04f}'.format(i=i, e=e)
'HUF.....315.8900....307.0900'

You can also separate the field width formatting from the float formatting like so: 您还可以将字段宽度格式与浮点格式分开,如下所示:

>>> '{:.<8}{:.<s}{:.>12}'.format(i, *[format(x, '.04f') for x in e])
'HUF.....315.8900....307.0900'

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

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