Keep two decimal digits in Python

Source: Internet
Author: User

I ran into a problem today when I wrote the program. about how to control floating-point numbers only two digits after the decimal point, the normal idea is to use the round function, such as round (A, 2), but in the face of the following problems round is not very useful

>>> a=13.949999999999999
>>> Round (A, 2)
13.949999999999999

On the Internet to check the information, a Netizen provides a method

>>> print "%.2f"% a 
13.95
You can also use the decimal

Decimal. The decimal type is a data type in Python that satisfies high-precision calculations, and imports the decimal package as needed

Define the decimal data type:

1 cannot be defined using the literal value of the assignment

2 define the way as follows:

>>> Import decimal >>> x = Decimal. Decimal (87345) >>> x decimal (' 87345 ') >>> x = Decimal. Decimal (' 123.3344332566334 ') >>> x decimal (' 123.3344332566334 ') can be passed to a decimal integer or string parameter, but not to floating-point data. Because the floating-point data itself is inaccurate

If you need to convert from floating-point data to a decimal type, you might use the following method >>> x = Decimal. Decimal.from_float (127.3323)
>>> x
Decimal (' 127.332300000000003592504072003066539764404296875 ')

Applies to integer operations also applies to decimal types

I have 3 questions pertaining to decimal arithmetic in Python, all 3 of which are best asked inline: 1)

>>> from decimal import getcontext, decimal
>>> getcontext (). Prec = 6
>>> decimal (' 50.567898491579878 ') * 1
Decimal (' 50.5679 ')
>>> # How are this a precision of 6? If the decimal counts whole numbers
as >>> # Part of the precision, was that actually still precision?

and 2)

>>> from decimal import getcontext, decimal
>>> getcontext (). Prec = 6
>>> decimal (' 50.567898491579878 ')
Decimal (' 50.567898491579878 ')
>>> # shouldn ' t that have been rounded to 6 digits On instantiation?
>>> decimal (' 50.567898491579878 ') * 1
decimal (' 50.5679 ')
>>> # Instead, it only follows my Precision setting set when operated on.
3)
>>> # now I want to save the ' value to ' my database as a ' total ' with 2 places.
>>> from decimal Import decimal >>> # are the following the correct way to get the '
value into 2 DECIM Al places,
>>> # or is there a "better" way?
>>> x = decimal (' 50.5679 '). Quantize (Decimal (' 0.00 '))
>>> x  # Just wanted to-= what the value W As
Decimal (' 50.57 ')
>>> foo_save_value_to_db (x)

Native Test Cases:
>>> Import Decimal
>>> x = Decimal. Decimal (87345)
>>> x
decimal (' 87345 ')
>>> print x
87345
>>> from Decimal import getcontext, decimal
>>> x = decimal (' 0.998531571219 '). Quantize (Decimal (' 0.00 '))
> >> x
Decimal (' 1.00 ')
>>> print x
1.00
>>> x = decimal (' 0.998531571219 '). Quantize (Decimal (' 0.0000 '))
>>> x
decimal (' 0.9985 ')
>>> print x
0.9985
>>> y = decimal.from_float (0.998531571219)
>>> y
Decimal (' 0.99853157121900004700165709436987526714801788330078125 ')
>>> y = decimal.from_float (0.998531571219 ). Quantize (Decimal (' 0.0000 '))
>>> y
decimal (' 0.9985 ')
>>> print y
0.9985
>>> f1 = 0.998531571219
>>> F1
0.998531571219
>>> type (F1)
<type ' float ' >
>>> f2 = str (f1)
>>> f2
' 0.998531571219 '
>>> Type (F2)
<type ' str ' >


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.