how do you get a float variable to round the decimal to two decimal places in python3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how do you get a float variable to round the decimal to two decimal places in python3

I'm having a bit of trouble with this and I have no idea what to do

9th Jan 2019, 7:43 PM
Austin
Austin - avatar
4 Answers
+ 6
Sample, print(round(1.23456,2))
9th Jan 2019, 7:53 PM
Александр Иванов
Александр Иванов - avatar
+ 6
Or print(f'{x:.2f}')
11th Jan 2019, 12:15 PM
David Ashton
David Ashton - avatar
+ 1
Another possibility print('{0:.2f}'.format(x)) https://code.sololearn.com/cOLsJ1p6zd15/?ref=app
10th Jan 2019, 3:34 PM
Tibor Santa
Tibor Santa - avatar
0
Use "round" in the following syntax: print ( round ( [NUM TO ROUND], [NUM OF DEC PLACES] ) ) Here is an example: >>> x = 1.23456789 >>> print(round(x, 2)) 1.23 >>> Hope that helps
13th Jan 2019, 9:43 AM
D. Booey 📜
D. Booey 📜 - avatar