Home >Backend Development >Python Tutorial >How to limit floating point type to two decimal places in python

How to limit floating point type to two decimal places in python

silencement
silencementOriginal
2019-05-25 13:12:2625417browse

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:

How to limit floating point type to two decimal places in python

2, characters String formatting method

How to limit floating point type to two decimal places in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more