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
Release: 2019-05-25 16:20:41
Original
25418 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template