环境:

os: win7 64bit

python:2.7.5  32bit

对python四舍五入的解决方案

现象:

一般的四舍五入操作都是使用内置的round方法
 
In [14]: round(2.675,2)
Out[14]: 2.67
文档中这样解释的
The documentation for the built-in round() function says that it rounds to the nearest value, rounding ties away from zero. Since the decimal fraction 2.675 is exactly halfway between 2.67 and 2.68, you might expect the result here to be (a binary approximation to) 2.68. It’s not, because when the decimal string 2.675 is converted to a binary floating-point number, it’s again replaced with a binary approximation, whose exact value is
In [22]: Decimal(2.675)
Out[22]: Decimal('2.67499999999999982236431605997495353221893310546875')

所以对于精度有明确要求的数学计算来说,使用round是不行的

google中有人这么解决的:

>>> from decimal import Decimal
>>> n = Decimal('1.555')
>>> round(n, 2)
Decimal('1.56')

##但是这个方法在2.7.5中已经不再适用了

现在使用的方式是:

可以使用str.format来格式化数字实现四舍五入
 from decimal import Decimal
In [15]: '{:.2f}'.format(Decimal('2.675'))
Out[15]: '2.68''

错误的写法
In [12]: '{:.2f}'.format(2.675)
Out[12]: '2.67'
In [13]: '{:.2f}'.format('2.675')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

In [14]: '{:.2f}'.format(Decimal(2.675))
Out[14]: '2.67'

[Python]round四舍五入精度缺失的解决的更多相关文章

  1. 关于在Android或Java中精度缺失的解决方法

    left,right是两个String类型的字符串,myres是一个double类型的变量. 如果我们用下面的语句把left,right先转换为double后直接加法的话,如果作3.3乘3之类的运算( ...

  2. js中toFixed精度问题的解决办法

    toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.例如将数据Num保留2位小数,则表示为:toFixed(Num):但是其四舍五入的规则与数学中的规则不同,使用的是银行家舍入规 ...

  3. Python numpy 浮点数精度问题

    Python numpy 浮点数精度问题 在复现FP(fictitious play, Iterative solution of games by fictitious play-page393)算 ...

  4. [Done]SnowFlake生成Long类型主键返回前台过长导致精度缺失的问题

    问题描述: 在开发过程中,项目的主键生成器是SnowFlake,其生成的long主键是28位, 但是js中Long的最大值:https://blog.csdn.net/sunmerZeal/artic ...

  5. 关于JS精度缺失问题

    问题描述 在Java后端传一个比较大的Long值的时候 前端接收值的时候会出现精度的缺失: 解决办法 添加一个转换类 点击查看代码 public class JacksonObjectMapper e ...

  6. 转:python idle 清屏问题的解决

    http://www.cnblogs.com/maybego/p/3234055.html python idle 清屏问题的解决 在学习和使用python的过程中,少不了要与python idle打 ...

  7. $ sudo python -m pip install pylint 出错解决方法

    问题:在unbuntu执行$ sudo python -m pip install pylint出错解决方法支行以下命令sudo pip install pylint==1.9.3这样roboware ...

  8. python错误日志记录工具,解决项目排错问题

    我们写项目的时候难免会遇到代码报错的问题,遇到这样的问题了如何快速的定位问题并解决问题呢? 我今天来整理了利用python只带的工具来解决这个问题,我能需要使用的库有: logging os 这些都是 ...

  9. python 安装pytorch 及 安装失败解决办法

    python 安装pytorch 及 安装失败解决办法 [转] pytorch安装失败解决办法 [转] 一分钟在win10终端成功安装pytorch pytorch 的安装方法有2种,一种是pip安装 ...

随机推荐

  1. 动态规划+滚动数组 -- POJ 1159 Palindrome

    给一字符串,问最少加几个字符能够让它成为回文串. 比方 Ab3bd 最少须要两个字符能够成为回文串 dAb3bAd 思路: 动态规划 DP[i][j] 意味着从 i 到 j 这段字符变为回文串最少要几 ...

  2. RHEL7下PXE+FTP+Kickstart无人值守安装操作系统

    1.配置yum源 vim /etc/yum.repos.d/development.repo [development] name=yum server baseurl=file:///mnt ena ...

  3. 比较好的总结runtime

    http://www.cocoachina.com/ios/20160128/15154.html

  4. python 整型--《Python 3程序开发指南》笔记

    参考:<Python 3程序开发指南> 整数转换函数: bin(i) 返回整数i的二进制表示(字符串) hex(i) 返回i的十六进制表示(字符串) int(x) 将x转换为整数,失败产生 ...

  5. css10定位属性

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. 转载-- C/S 与 B/S 区别

    C/S 与 B/S 区别   C/S结构,即Client/Server(客户机/服务器)结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,可 ...

  7. url的非法字符有哪些

    需要过滤的特殊字符及字符串有: net user xp_cmdshell /add exec master.dbo.xp_cmdshell net localgroup administrators ...

  8. ods_yx给用户分配表空间、权限用户等工作内容。

    1.登陆运维审计 huang_cb.bl hac12345 2.找到81.35   root-admin  nwsj*2013 3.打开oracle EMC工具,使用ods_yx用户登陆进EMC里面的 ...

  9. 打开的IE网页不是最大化的解决方法

    方法一:先把所有的IE窗口关了;只打开一个IE窗口;最大化这个窗口;关了它;OK,以后的默认都是最大化的了 方法二:先关闭所有的IE浏览器窗口,用鼠标右键点击快速启动栏的IE浏览器图标,在出现的快捷菜 ...

  10. 拿起cl.exe,放下IDE

    笔者在这里介绍一种使用cl.exe编译源文件的方法,可以手动执行编译过程而不再依赖IDE,此外,笔者还介绍一些使用cl.exe编译简单源代码的方式. cl.exe是windows平台下的编译连接程序, ...