# -*- 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. Android两种 旋转Bitmap方法

    方法1. 利用Bitmap.createBitmap Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {      ...

  2. [React Testing] Setting up dependencies && Running tests

    To write tests for our React code, we need to first install some libraries for running tests and wri ...

  3. c++11 : Local and Unnamed Types as Template Arguments

    In N2402, Anthony Williams proposes that local types, and unnamed types be usable as template argume ...

  4. USB HID Report Descriptor 报告描述符详解

    Report descriptors are composed of pieces of information. Each piece of information is called an Ite ...

  5. Python序列的方法(转)

    在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值表(tuple)和表(list).此外,字符串(string)是一种特殊的定值表.表的元素可以更改,定值表一旦建立,其 ...

  6. htm初学笔记

    一.什么是html HTML(HyperText Markup Language):超文本标记语言,一种纯文本类型的语言 --使用带有尖括号的“标记”将网页中的内容逐一标识出来 用来设计网页的标记语言 ...

  7. 127.0.0.1与localhost与ip的区别

    127.0.0.1与localhost与ip的区别 May 18, 2014 localhost 不联网不使用网卡,不受防火墙和网卡限制本机访问 一般使用 本地套接字文件AF_UNIX 应用程序一般约 ...

  8. 矩阵链乘 hrbust 1600

    #include<string.h> //区间dp的思想#include<iostream> //将一个区间分成两段,将每一段当成是一个矩阵#include<stdio. ...

  9. 解决CentOS 5.8在虚拟机环境下如何桥接上网

    1.虚拟机的网卡配置如下图所示: 2.在CentOS 5.8的命令行界面:输入如下指令 然后准备修改里面的网关地址和自己的IP地址 3.同时查看自己的IP地址和网关 4.在第二步里面修改,网关地址应该 ...

  10. Google Guava的splitter用法

    google的guava库是个很不错的工具库,这次来学习其spliiter的用法,它是一个专门用来 分隔字符串的工具类,其中有四种用法,分别来小结 1 基本用法: String str = " ...