Home >Backend Development >Python Tutorial >How to keep two decimal places in python output

How to keep two decimal places in python output

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-01 16:20:4354141browse

How to keep two decimal places in python output

Four ways to output python with two decimal places:

a = 5.5461

Method 1: round(a ,2)

How to keep two decimal places in python output

Method 2: float('%.2f' % a)

How to keep two decimal places in python output

##Related recommendations: "

Python video tutorial

Method three: '%.2' %a

How to keep two decimal places in python output##Method four:

from decimal import Decimal

Decimal('5.026').quantize(Decimal('0.00'))

How to keep two decimal places in python outputWhen the output result needs to have two decimal places When using string format: '%.2f' % a is the best method, followed by Decimal.

Note:

1. It can be passed to Decimal integer or string parameters, but it cannot be floating point data, because floating point data itself is not accurate.

2. Decimal can also be used to limit the total number of digits in data.

The above is the detailed content of How to keep two decimal places in python output. 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