Home > Backend Development > Python Tutorial > Python uses the format method to retain three decimal places

Python uses the format method to retain three decimal places

王林
Release: 2020-05-10 13:49:41
Original
18841 people have browsed it

Python uses the format method to retain three decimal places

# The format method is a built-in Python string formatting method.

The basic syntax is: str.format(), which enhances the string formatting function.

The basic syntax is to use {} and : to replace the previous %.

The format function can receive multiple parameters, and the positions do not need to be in order.

Specific examples are as follows:

>>> print("{:.0f}".format(3.1415926))
3
>>> print("{:.2f}".format(3.1415926))
3.14
>>> print("{:.3f}".format(3.1415926))
3.142
Copy after login

Recommended tutorial: python tutorial

The above is the detailed content of Python uses the format method to retain three decimal places. 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