今天写程序的时候碰到了一个问题关于如何控制浮点数只显示小数点后两位,正常的想法是用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'
()>>>本机测试用例:
  1. >>> import decimal
  2. >>> x = decimal.Decimal(87345)
  3. >>> x
  4. Decimal('87345')
  5. >>> print x
  6. 87345
  7. >>> from decimal import getcontext, Decimal
  8. >>> x = Decimal('0.998531571219').quantize(Decimal('0.00'))
  9. >>> x
  10. Decimal('1.00')
  11. >>> print x
  12. 1.00
  13. >>> x = Decimal('0.998531571219').quantize(Decimal('0.0000'))
  14. >>> x
  15. Decimal('0.9985')
  16. >>> print x
  17. 0.9985
  18. >>> y = Decimal.from_float(0.998531571219)
  19. >>> y
  20. Decimal('0.99853157121900004700165709436987526714801788330078125')
  21. >>> y = Decimal.from_float(0.998531571219).quantize(Decimal('0.0000'))
  22. >>> y
  23. Decimal('0.9985')
  24. >>> print y
  25. 0.9985
  26. >>> f1 = 0.998531571219
  27. >>> f1
  28. 0.998531571219
  29. >>> type(f1)
  30. <type 'float'>
  31. >>> f2 = str(f1)
  32. >>> f2
  33. '0.998531571219'
  34. >>> type(f2)
  35. <type 'str'>
  36. >>>

python中保留两位小数的更多相关文章

  1. Python中保留两位小数的几种方法

    https://blog.csdn.net/Jerry_1126/article/details/85009810 保留两位小数,并做四舍五入处理方法一: 使用字符串格式化>>> a ...

  2. Oracle中保留两位小数

    在最近的项目开发中,有个业务需求是界面显示的数字需要保留两位小数,目前我想到的解决方法有两种: (1)在写SQL的时候,直接保留两位小数 (2)在java代码里面将查询出来的数进行格式化处理,保留两位 ...

  3. C#中保留两位小数但不四舍五入的最优做法

    多种做法比较 class Program_保留两位小数但不四舍五入 { static void Main(string[] args) { Helper.Run(delegate () { metho ...

  4. java中保留两位小数的方法

    1.BigDecimal的setScale // RoundingMode 舍入模式: // 1.UP/DOWN ->基准为数字0: // 2.CEILING/FLOOR ->基准为正负无 ...

  5. Office EXCEL 如何保留两位小数,四舍五入

    选中若干单元格,然后右击设置单元格格式,数值中保留两位小数   使用round函数四舍五入,如下图所示,我在N10单元格中输入"ROUND(M10,1)"即可,其中ROUND是函数 ...

  6. python(62):保留两位小数

    转载:https://blog.csdn.net/jiandanjinxin/article/details/77752297 在C/C++语言对于整形数执行除法会进行地板除(舍去小数部分). 例如 ...

  7. 字符串怎么换行 || 字符串中使用单引号时应该怎么写 || 保留两位小数 || 数字0在if中的意思是false || 什么情况下会会报undefined || null和undefined的区别 ||

    换行的字符串 "This string\nhas two lines" 字符串中使用单引号时应该怎么写 'You\'re right, it can\'t be a quote' ...

  8. 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) ...

  9. JS中格式化数据保留两位小数

    问题:在JS中格式化数据保留两位小数的函数的多种方法 最好方法: 保留两位好像是这样吧     var   a   =   9.39393;     alert(a.toFixed(2)); 说明: ...

随机推荐

  1. 学生信息管理系统v1.0

    昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙.需要统计处理很多学生信息,想让我帮他做一个管理系统.实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到 ...

  2. js动态设置窗体位置

    1设置登录框的js,动态设置高度等 <script> $(document).ready(function () { $() / + "px"); $("in ...

  3. INNO:检测程序是否已经安装,是则弹出卸载提示。

    INNO:检测程序是否已经安装,是则弹出卸载提示. 作者:少轻狂 | 发布:2010-08-05 | 更新:2013-09-05 | 分类:部署 | Disposition | 热度:2816 ℃ 实 ...

  4. 了解magento数据库

    网址是:http://www.magereverse.com/,不同版本都有

  5. Magento架构师的笔记-----Magento显示当前目录的父分类和子分类的分类名

    在Magento目录的分类页面里,希望在左侧导航获取到父分类和子分类,可以用以下方法:打开app/your_package/your_themes/template/catalog/navigatio ...

  6. 【题解】【排列组合】【素数】【Leetcode】Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  7. CGI实现页面的动态生成

    传统的Web应用开发局限于有限的静态页面(HTML静态页面),不利于系统的扩展,不能提供及时信息,而且修改维护麻烦,所以建立一个动态Web应用程序尤为重要.一方面根据访问者的不同请求返回不同的访问信息 ...

  8. RG100A-AA 中大校园网上网及远程配置

    由于无线网卡用得不爽,wifi经常断,所以想整个路由器,造福群众.在朋友介绍下购得一台已经刷好 Openwrt 的上海贝尔RG100A-AA路由器,根据下面的简单步骤,就能连接上校园网. 一.准备工作 ...

  9. python 函数基础

    定义: def intersect(seq1,seq2): res = [] for x in seq1: if x in seq2: res.append(x) yield res 运行效果 > ...

  10. ZOJ 1024 Calendar Game

    原题链接 题目大意:2001年11月4日是一个黄道吉日,有两个人做游戏,看谁先数到这个日子.可以往后数一天,比如2号到3号,30号到31号,也可以往后数一个月.给出一个起始的日子,求能否保证先开始游戏 ...