简体   繁体   中英

Making result of division a float with 2 decimal places (Python)

I know this is a noob question but, is it possible to make the result of a division a float with 2 decimal places?

For example when dividing 50 by 2 the result is 25.0. When divided by 3 the result is 16.666666666666668. Can I force both results to be a float with 2 decimal places?

Thanks, and sorry!

You can use something like:

x = 16.666666666
rounded_x = format(x, '.2f') #This will be a string
>>> round(16.666666666666668,2)
16.67

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM