How to keep two decimal places in python output

爱喝马黛茶的安东尼
Release: 2019-08-01 16:20:43
Original
54079 people have browsed it

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!