Python module: decimal

Source: Internet
Author: User
Tags pprint

#-*-Coding:utf-8-*-
__author__ = ' Administrator '
#数学计算
Import decimal# for fixed-point and floating-point arithmetic
#文档: Https://docs.python.org/2.7/library/decimal.html?highlight=decimal#module-decimal
#使用from_float () can accurately convert decimals
Fmt= ' {0:<25} {1:<25} '
Print Fmt.format (' INPUT ', ' output ')
Print Fmt.format ('-' *25, '-' *25)
Print Fmt.format (5,decimal. Decimal (5))
Print Fmt.format (' 3.14 ', decimal. Decimal (' 3.14 '))
Print Fmt.format (repr (. 1), Decimal. Decimal (str (. 1)))
f=0.1
Print Fmt.format ('%.23g '%.1,str (decimal. Decimal.from_float (f)) [: 25])
#decimal. Decimal can also be tuples, 0 for positive, 1 for negative, numeric tuple, and an integer exponent
T= (1, (+),-2)
print ' INPT: ', t
Print Decimal. Decimal (t)
#说明: is a portable way to export small values without damaging precision, tuples are transferred over the network, or stored in an exact fractional database is not supported
#计算 (Give a few simple examples)
Print
A=decimal. Decimal (' 2 ')
B=decimal. Decimal (' 1.2 ')
Print Str (a), str (b)
Print A+b,a-b,b*a,a/b,a%b
#特殊值
Print
For TSZ in [' Infinity ', ' NaN ', ' 0 ']:
Print Decimal. Decimal (TSZ), Decimal. Decimal ('-' +tsz)
Print Decimal. Decimal (' NaN ') ==decimal. Decimal (' Infinity ')
#上下文
Print
#获取当前全局上下文, GetContext ()
Import Pprint
Con=decimal.getcontext ()
Print con. Emax
Print con. Emin
Print con.capitals#1
Print con.prec#28
Print Con.rounding#round_half_even
Pprint.pprint (Con.traps)
#返回以下内容:
"""
{<class ' decimal. Clamped ';: 0,
<class ' Decimal. InvalidOperation ';: 1,
<class ' Decimal. Divisionbyzero ';: 1,
<class ' Decimal. Inexact ';: 0,
<class ' Decimal. Rounded ';: 0,
<class ' Decimal. Subnormal ';: 0,
<class ' Decimal. Overflow ';: 1,
<class ' Decimal. Underflow ';: 0}
"""

Python module: decimal

Related Article

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.