Python print out float or integer

后端 未结 4 1410
难免孤独
难免孤独 2020-12-20 00:30

How can i print out float if the result have decimal or print out integer if the result have no decimal?

c = input(\"Enter the total cost of purchase: \")
ba         


        
4条回答
  •  难免孤独
    2020-12-20 01:02

    Python floats have a built-in method to determine whether they're an integer:

    x = 212.50
    y = 212.0
    f = lambda x: int(x) if x.is_integer() else x
    print(x, f(x), y, f(y), sep='\t')
    >> 212.5    212.5   212.0   212
    

提交回复
热议问题