Home > Article > Backend Development > How to limit floating point type to two decimal places in python
Python controls the number of decimal places after the decimal point. There are three solutions
1. Use the round() function
Example: x=1.23456, you only want to keep 3 digits after the decimal point when outputting:
2, characters String formatting method
3, format() method
x =1.23456 print("{:.3f}".format(x))
#{} represents the slot, and subsequent variables are filled into the slot
#{:.3f} represents x When filling into the slot, take 3 decimal places
The above is the detailed content of How to limit floating point type to two decimal places in python. For more information, please follow other related articles on the PHP Chinese website!