简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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