how do you get a float variable to round the decimal to two decimal places in python3 | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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