python中保留两位小数
今天写程序的时候碰到了一个问题关于如何控制浮点数只显示小数点后两位,正常的想法是用round函数,例如 round(a, 2),但是在面对下面的问题时候round就不太好用了
>>> a=13.949999999999999
>>> round(a, 2)
13.949999999999999
上网查了资料,有网友提供了一种方法
print('%.2f'%a)
>>>13.95
另外还可以用 trunc(a,2)截取函数
>>>13.94
还可以使用decimal
decimal.Decimal类型是Python中满足高精度计算的一种数据类型,使用进需要导入decimal包
定义Decimal数据类型:
1 无法使用赋字面值的方式定义
2 定义方式如下:
>>> import decimal >>> x = decimal.Decimal(87345) >>> x Decimal('87345') >>> x = decimal.Decimal('123.3344332566334') >>> x Decimal('123.3344332566334') 可以传递给Decimal整型或者字符串参数,但不能是浮点数据,因为浮点数据本身就不准确
如果需要从浮点数据转换为Decimal类型,可能使用如下方法
>>> x = decimal.Decimal.from_float(127.3323)
>>> x
Decimal('127.332300000000003592504072003066539764404296875')
I have 3 questions pertaining to decimal arithmetic in Python, all 3 of which are best asked inline:
1)
fromimport,Decimal>>>().=6>>>Decimal'50.567898491579878' 1Decimal'50.5679'
# How is this a precision of 6? If the decimal counts whole numbers as>>># part of the precision, is that actually still precision?>>>and
2)
fromimport,Decimal>>>().=6>>>Decimal'50.567898491579878'
Decimal'50.567898491579878'
# Shouldn't that have been rounded to 6 digits on instantiation?>>>Decimal'50.567898491579878' 1Decimal'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.>>>fromimportDecimal>>># Is the following the correct way to get the value into 2 decimal places,>>># or is there a "better" way?>>>=Decimal'50.5679'quantizeDecimal'0.00'
# Just wanted to see what the value wasDecimal'50.57'
()>>>本机测试用例:
- >>> 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'>
- >>>
python中保留两位小数的更多相关文章
- Python中保留两位小数的几种方法
https://blog.csdn.net/Jerry_1126/article/details/85009810 保留两位小数,并做四舍五入处理方法一: 使用字符串格式化>>> a ...
- Oracle中保留两位小数
在最近的项目开发中,有个业务需求是界面显示的数字需要保留两位小数,目前我想到的解决方法有两种: (1)在写SQL的时候,直接保留两位小数 (2)在java代码里面将查询出来的数进行格式化处理,保留两位 ...
- C#中保留两位小数但不四舍五入的最优做法
多种做法比较 class Program_保留两位小数但不四舍五入 { static void Main(string[] args) { Helper.Run(delegate () { metho ...
- java中保留两位小数的方法
1.BigDecimal的setScale // RoundingMode 舍入模式: // 1.UP/DOWN ->基准为数字0: // 2.CEILING/FLOOR ->基准为正负无 ...
- Office EXCEL 如何保留两位小数,四舍五入
选中若干单元格,然后右击设置单元格格式,数值中保留两位小数 使用round函数四舍五入,如下图所示,我在N10单元格中输入"ROUND(M10,1)"即可,其中ROUND是函数 ...
- python(62):保留两位小数
转载:https://blog.csdn.net/jiandanjinxin/article/details/77752297 在C/C++语言对于整形数执行除法会进行地板除(舍去小数部分). 例如 ...
- 字符串怎么换行 || 字符串中使用单引号时应该怎么写 || 保留两位小数 || 数字0在if中的意思是false || 什么情况下会会报undefined || null和undefined的区别 ||
换行的字符串 "This string\nhas two lines" 字符串中使用单引号时应该怎么写 'You\'re right, it can\'t be a quote' ...
- python保留两位小数
python保留两位小数: In [1]: a = 5.026 In [2]: b = 5.000 In [3]: round(a,2) Out[3]: 5.03 In [4]: round(b,2) ...
- JS中格式化数据保留两位小数
问题:在JS中格式化数据保留两位小数的函数的多种方法 最好方法: 保留两位好像是这样吧 var a = 9.39393; alert(a.toFixed(2)); 说明: ...
随机推荐
- 转:去掉DataTable重复数据(程序示例比较)
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...
- cron用法
cron用法说明 cron的用法老是记不住,索性写下来备忘.下文内容大部分是根据<Cron Help Guide>翻译而来,有些部分是自己加上的. 全文如下: cron来源于希腊单词chr ...
- JAVA存取PG大对象类型OID数据
转载地址:http://my.oschina.net/liuyuanyuangogo/blog/151537 pg用大对象存储二进制数据的老文档:http://jdbc.postgresql.org/ ...
- ZOJ 1216 Deck
原题链接 题目大意:1/2+1/4+1/6+…1/n 解法:直接累加即可. 参考代码: #include<stdio.h> int main(){ printf("# Cards ...
- 在web.xml中配置error-page
在web.xml中配置error-page 在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下: 一. 通过错误码来配置error ...
- POJ2391 Ombrophobic Bovines(网络流)(拆点)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- P235 实战练习(集合类2)、摇奖程序和验证码(修改版)
1.分别向Set集合以及List集合中添加“A”.“a”.“c”.“C”.“a”5个元素,观察重复值“a”能否在List集合以及Set集合中成功添加. package org.hanqi.practi ...
- iOS的一些坑
1.动态绑定元素,不能绑定到body上 $(document).on('click','#nuse-content .coupon',fn) 这样是不行的,会导致事件无法触发,除非给要绑定时间的元素显 ...
- Swiper说明及API手册说明
最近使用Swipe.js,发现中文的资料很少,试着翻译了一下.能力有限,翻译难免错漏,欢迎指出,多谢! 翻译自:http://www.idangero.us/sliders/swiper/api.ph ...
- Codeforces Round #339 Div.2 C - Peter and Snow Blower
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. A ...