The reason for this error is that the format specifier d is specifically for formatting integers, not floats.You can use the general format specifier f instead.
f'{int(1223.555):5d}'
Answer from 911 on Stack Overflowf"..." expressions format values to strings, integrate the result into a larger string and return that result. That's not quite the same as 'casting'*.
number is an expression here, one that happens to produce an integer object. The integer is then formatted to a string, by calling the __format__ method on that object, with the first argument, a string containing a format specifier, left empty:
>>> number = 10
>>> number.__format__('')
'10'
We'll get to the format specifier later.
The original integer object, 10, didn't change here, it remains an integer, and .__format__() just returned a new object, a string:
>>> f"{number} is not a string"
'10 is not a string'
>>> number
10
>>> type(number)
int
There are more options available to influence the output by adding a format specifier to the {...} placeholder, after a :. You can see what exact options you have by reading the Format Specification Mini Language documentation; this documents what the Python standard types might expect as format specifiers.
For example, you could specify that the number should be left-aligned in a string that's at least 5 characters wide, with the specifier <5:
>>> f"Align the number between brackets: [{number:<5}]"
'Align the number between brackets: [10 ]'
The {number:<5} part tells Python to take the output of number.__format__("<5") and use that as the string value to combine with the rest of the string, in between the [ and ] parts.
This still doesn't change what the number variable references, however.
*: In technical terms, casting means changing the type of a variable. This is not something you do in Python because Python variables don't have a type. Python variables are references to objects, and in Python it is those objects that have a type. You can change what a variable references, e.g. number = str(number) would replace the integer with a string object, but that's not casting either.
You can test it yourself quite easily
number = 10
print(f"{number} is not a string")
print(type(number))
# output is integer
Include the type specifier in your format expression:
>>> a = 10.1234
>>> f'{a:.2f}'
'10.12'
When it comes to float numbers, you can use format specifiers:
f'{value:<width>.<precision>}'
where:
valueis any expression that evaluates to a numberwidthspecifies the number of characters used in total to display, but ifvalueneeds more space than the width specifies then the additional space is used.precisionindicates the number of characters used after the decimal point
What you are missing is the type specifier for your decimal value. In this link, you an find the available presentation types for floating point and decimal.
Here you have some examples, using the f (Fixed point) presentation type:
# notice that it adds spaces to reach the number of characters specified by width
In [1]: f'{1 + 3 * 1.5:10.3f}'
Out[1]: ' 5.500'
# notice that it uses more characters than the ones specified in width
In [2]: f'{3000 + 3 ** (1 / 2):2.1f}'
Out[2]: '3001.7'
In [3]: f'{1.2345 + 4 ** (1 / 2):9.6f}'
Out[3]: ' 3.234500'
# omitting width but providing precision will use the required characters to display the number with the the specified decimal places
In [4]: f'{1.2345 + 3 * 2:.3f}'
Out[4]: '7.234'
# not specifying the format will display the number with as many digits as Python calculates
In [5]: f'{1.2345 + 3 * 0.5}'
Out[5]: '2.7344999999999997'