环境:

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. JavaScript数组的学习

    1数组的创建 var arr1 = new Array(); var arr2=[1,2,3]; 2数组常用的方法: push,pop,shift,unshift,splice,slice,sort, ...

  2. JAVA - 回调机制

    参考例子:android的Button OnClickListener接口.<第一行代码>中的回调例子   定义接口 public interface HttpCallbackListen ...

  3. JAVA虚拟机与内存

    资料整理自网络(侵删) JVM内存 组成 JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 栈区: 1.每个线程包含一个栈区,栈中只保存基础数据类型的对象和 ...

  4. 用Hexo搭建属于自己的Blog

    什么是Hexo 简单的来说,Hexo是一款基于Node.JS的静态博客框架,官方给它的描述是"A fast, simple & powerful blog framework&quo ...

  5. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

    2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North ...

  6. oracle的concat的用法

    select concat( concat( concat( ( select area_name from ec_area where area_id ), ( select area_name f ...

  7. SparseArray HashMap 稀疏数组 二分法

    简介 HashMap是java里比较常用的一个集合类,我们常用其来缓存一些处理后的结果,但是在Android项目中,Eclipse却给出了一个 performance 警告.意思就是说用SparseA ...

  8. 你以为你了解最常用的string.substring()的几个常见问题吗?

    ---恢复内容开始--- 前言: 1.项目中我们难免会用到各种对字符串的处理方法,可是你曾知道substring()这个用法别有洞天?你考虑过一下几个情况吗? 使用Substring()时的正确写法: ...

  9. 认识html标签

    让我们通过一个网页的学习,来对html标签有一个初步理解. 平常大家说的上网就是浏览各种各式各样的网页,这些网页都是由html标签组成的. 下面就是一个简单的网页.效果图如下: 我们来分析一下,这个网 ...

  10. 三维FEM的刚度矩阵数量级

    刚刚在调试三维的FEM代码,随手把全局刚度矩阵在FULL的情况下保存到了dat文件里. 注意,这个仅仅是一个半径为十的球的刚度矩阵 居然3.6G!!吓坏了!!截个图,留个纪念.还是老老实实用spars ...