How Can I Remove ".0" Of Float Numbers?
Say I have a float number. If it is an integer (e.g. 1.0, 9.0, 36.0), I want to remove the '.0 (the decimal point and zero)' and write to stdout. For example, the result will be 1,
Solution 1:
Here's a function to format your numbers the way you want them:
defformatNumber(num):
if num % 1 == 0:
returnint(num)
else:
return num
For example:
formatNumber(3.11111)
returns
3.11111
formatNumber(3.0)
returns
3
Solution 2:
You can combine the 2 previous answers :
formatNumber = lambda n: n if n%1elseint(n)
>>>formatNumber(5)
5
>>>formatNumber(5.23)
5.23
>>>formatNumber(6.0)
6
Solution 3:
Solution 4:
You can do that with fstrings like
print(f'{1.0:g},{1.2:g}') # Output: 1,1.2
Solution 5:
just type int(number) example :
int(3.0)
returns
3
Post a Comment for "How Can I Remove ".0" Of Float Numbers?"