2

Very quick question.

x = 10
print("value is {:d}".format(x))

returns

value is 10

on the other hand:

x = 10.0
print("value is {:d}".format(x))

returns

ValueError: Unknown format code 'd' for object of type 'float'

Why doesnt this work?

Andrew Zaw
  • 754
  • 1
  • 9
  • 16

2 Answers2

1

You would use f not d for floats. And then specify the precision width as 0:

>>> print("value is {:.0f}".format(x))
value is 10
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
1

From Python docs: 'd' Decimal Integer. Outputs the number in base 10. It will output the number in base 10, thats why you are getting the ValueError.

andrew
  • 4,991
  • 5
  • 24
  • 27