+ 1
How to output a float with only 2 decimals in my code
https://code.sololearn.com/cINCb5YEt3we/?ref=app My code have no bugs, but I want to output a float that have only 2 decimal numbers Any way to achieve that? Btw The program is written in Python
4 Respostas
+ 6
You can use the round(float, number_of_decimals) function
For example:
round(8.472848, 2) returns 8.47
round(8.472848, 5) returns 8.47285
Do you understand?
+ 7
CODING NINJA ,
we can also use string interpolation (f-strings)
num = 8.472848
print(f'{num} rounded to 2 decimal places is: {num:.2f}')
print(f'{num} rounded to 5 decimal places is: {num:.5f}')
output is:
8.472848 rounded to 2 decimal places is: 8.47
8.472848 rounded to 5 decimal places is: 8.47285
+ 2
Ugulberto Sánchez Yes I understand, Thanks
+ 1
Your welcome