Home > Software engineering >  How do I add another 0 in my float output "$7.0" using format() in Python
How do I add another 0 in my float output "$7.0" using format() in Python

Time:02-25

def cash_converter():
        integer = input("enter an integer: ")  # Prompt user for integer input: 7
        sentence = "That is ${}".format(float(integer))
        print(sentence)

My Output:

enter an integer: 7

That is $7.0

Expected Output:

enter an integer: 7

That is $7.00

CodePudding user response:

(:."number"f) will determine the number of deicimels.

Change

sentence = "That is ${}".format(float(integer))

to

sentence = "That is ${:.2f}".format(float(integer))

CodePudding user response:

There are a few ways that you can do this the way that I would do it is like this

print(format(7, ",.2f"))

This will format the value to the 2 decimal place.

  • Related