How to output a float with only 2 decimals in my code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

13th Apr 2023, 3:23 PM
Narūto Primē
Narūto Primē - avatar
4 Answers
+ 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?
13th Apr 2023, 3:35 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 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
13th Apr 2023, 4:43 PM
Lothar
Lothar - avatar
+ 2
Ugulberto Sánchez Yes I understand, Thanks
13th Apr 2023, 3:46 PM
Narūto Primē
Narūto Primē - avatar
+ 1
Your welcome
13th Apr 2023, 3:46 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar