Add 0 after the comma

Asked

Viewed 172 times

0

I have a string str(owner.Vr_real).replace('.',',') which receives values of the type:

  • 2045.5
  • 3040,45
  • 4042,05

Note that the first number does not complete with 0 (ex: 2045.50). What function could I use to add 0 at the end? I tried using zfill, but without success.

1 answer

1


You need to take a look at String Formatting Operations.

I realize that you want to first exchange the dots by comma and then set the number of decimals. I suggest you do the opposite. First set the number of decimals and then turn the points into commas.

You should use:

str("%0.2f" % (owner.Vr_real)).replace('.',',')
  • Yes, the first example of the documentation would have already answered my question: >>> print '%(language)s has %(number)03d quote types. ' % ... {"language": "Python", "number": 2} Python has 002 quote types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.