# -*- coding: utf-8 -*-
__author__ = 'Administrator'
#数学计算
import decimal#用于定点和浮点运算
#文档:https://docs.python.org/2.7/library/decimal.html?highlight=decimal#module-decimal
#使用from_float()可以精确的把小数进行转换
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还可以元组,0表示正的,1表示负的,数字tuple和一个整数指数
t=(1,(1,1),-2)
print 'inpt:',t
print decimal.Decimal(t)
#说明:是一种可移植的方式,可以导出小数值而且不会损坏精度,元组在网络上传输,或者在不支持精确小数数据库中存储
#计算(给几个简单的例子)
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之模块:decimal的更多相关文章

  1. python基础-模块

    一.模块介绍                                                                                              ...

  2. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  3. Day5 模块及Python常用模块

    模块概述 定义:模块,用一砣代码实现了某类功能的代码集合. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,提供了代码的重用性.在Python中,一个.py文件就称之为一个模块(Mod ...

  4. Day6 模块及Python常用模块

    模块概述 定义:模块,用一砣代码实现了某类功能的代码集合. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,提供了代码的重用性.在Python中,一个.py文件就称之为一个模块(Mod ...

  5. python float转为decimal

    73.2413793103 ======= 73.2414 <type 'float'> ======= <class 'decimal.Decimal'> 当断言这两个值相等 ...

  6. Python常用模块-时间模块(time&datetime)

    Python常用模块-时间模块(time & datetime) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.初始time模块 #!/usr/bin/env pyth ...

  7. Python标准模块--threading

    1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...

  8. Python的模块引用和查找路径

    模块间相互独立相互引用是任何一种编程语言的基础能力.对于“模块”这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义.对于编译型的语言,比如C#中 ...

  9. Python Logging模块的简单使用

    前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...

  10. Python标准模块--logging

    1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...

随机推荐

  1. Unity3D性能优化总结

    一.程序方面 01.务必删除脚本中为空或不须要的默认方法: 02.仅仅在一个脚本中使用OnGUI方法. 03.避免在OnGUI中对变量.方法进行更新.赋值,输出变量建议在Update内. 04.同一脚 ...

  2. JSP小实例--计算器

    package cn.com.caculate; import java.math.BigDecimal; public class caculate { private String firstNu ...

  3. 百度之星资格赛 hdu 4826 Labyrinth 动态规划

    /********************* Problem Description 是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上 ...

  4. POJ3692 Kindergarten 【最大独立集】

    Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5317   Accepted: 2589 Desc ...

  5. HID Keyboard & Mouse descriptor.

    在USB中,USB HOST是通过各种描述符来识别设备的,有设备描述符,配置描述符,接口描述符,端点描述符,字符串描述符,报告描述符等等.USB报告描述符(Report Descriptor)是HID ...

  6. 206.反转单列表 Reverse Linked List

    Reverse a singly linked list. 使用栈 public class Solution { public ListNode ReverseList(ListNode head) ...

  7. Windows Bridge for iOS: Let’s open this up

    (原文翻译过来的,原文链接http://blogs.windows.com/buildingapps/2015/08/06/windows-bridge-for-ios-lets-open-this- ...

  8. IE9的window.showmodaldialog显示问题

    <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...

  9. xcode下载方式

    1.去AppStore下载 对于Xcode老是在AppStore升级失败,而且下载慢,可取找到了这个--> 官方 Xcode .dmg 文件下载链接:超级传送门 2.开发者中心官网下载 可参考这 ...

  10. IIS上部署网站404错误

    新装的系统上部署.net网站遇到403.404错误,可能原因记录: 1.应用程序池选择错误,一般选择4.0的 2.ASP.NET4.0应用程序池未安装(一般先安装了framework4.0,后安装ii ...