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
  • 642
  • 6
  • 15

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
  • 74,909
  • 8
  • 119
  • 129
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,171
  • 2
  • 22
  • 26