问题描述

在编写python代码时,进行小数位相减时出现 TypeError: unsupported operand type(s) for -: ‘decimal.Decimal’ and ‘float’ 的错误提示信息

原因分析

以上导致报错的原因是,双精度类型和浮点型相减报错

Python 内置类浮点数执行计算时,我们会惊奇发现计算结果与我们想象的完全不一样,比如我们都知道1.1和2.2的总和是3.3,但在Python中却不是这样的。

>>> (1.1 + 2.2) == 3.3
False
>>> 1.1 + 2.2
3.3000000000000003
>>>

解决方法

为了解决这个问题,我们可以使用Python附带的十进制模块。浮点数的精度最多为 15 位小数,而小数点数模块的精度可由用户设置。

from decimal import Decimalprint(0.1)
print(decimal.Decimal(0.1))

输出结果

>>> from decimal import Decimal
>>> print(0.1)
0.1
>>> print(Decimal(0.1))
0.1000000000000000055511151231257827021181583404541015625
>>>

修改python代码,如下所示:

# yx_jxxs = (jxxs - 1.1) / (0.15 * 12)  // TypeError: unsupported operand type(s) for -: 'decimal.Decimal' and 'float'
yx_jxxs = (jxxs - Decimal("1.1")) / Decimal(str(0.15*12))

运行代码,错误解决

python报错TypeError: unsupported operand type(s) for -: ‘decimal.Decimal‘ and ‘float‘的解决方法相关推荐

  1. 解决pip安装时出现报错TypeError: unsupported operand type(s) for -=: ‘Retry‘ and ‘int‘

    我在Linux下使用pip安装时出现报错: Exception: Traceback (most recent call last):File "/usr/lib/python2.7/dis ...

  2. 预测数据时数据类型是object导致报错TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘

    解决方法 更换数据类型: data:pd.DataFrame = data.astype('int64') # 或是: data:pd.DataFrame = data.astype('float') ...

  3. 解决pandas(Python)的报错:unsupported operand type(s) for -: ‘datetime.date’ and ‘Timestamp’

    在使用Pandas包的时候,遇到时间加减出现的报错 'datetime.date'是datetime的一种时间格式: 'Timestamp'(注意是大写的字母)是Pandas的一种时间格式. 这两个虽 ...

  4. python报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x97 in position的解决方法

    在编写代码时,调用python解释器中的模块时出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0x97 in position 20: ill ...

  5. opencv小笔记(TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘NoneType‘)

    今天在学习OpenCV的算数操作时,进行图像加法时,运行了下列程序 import numpy as np import cv2 as cv import matplotlib.pyplot as pl ...

  6. 成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'

    成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list' 目录 解决问题 解决思路 解决方法 解决问题 Typ ...

  7. 成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict'

    成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict' 目录 解决问题 解决思路 解决方法 解决问题 TypeE ...

  8. apex安装报错:TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘

    参考解决方法:TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' · Issue #990 · NVIDIA/apex ...

  9. Python PIP Install throws TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

    Python PIP Install throws TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

最新文章

  1. WordPress网站的问题
  2. 自编码器(Auto Encoder)原理及其python实现
  3. 在C#程序中实现插件架构
  4. C#如何使用DirectX实现视频播放
  5. 为input输入框添加圆角并去除阴影
  6. 实验15 安装新的int 9中断例程
  7. 如何在读研读博的道路上迅速失败?
  8. 2 字符串求交集_PostGIS教程十八:维数扩展的9交集模型
  9. 在线Excel文件解析转换成JSON格式
  10. TensorFlow精进之路(八):神经元
  11. kindle看pdf不清楚_Kindle 对 PDF 的支持真的很糟糕吗?
  12. 自学web前端课程大纲分享,适合所有人学习
  13. 扫读笔好还是点读笔好 有什么区别
  14. 云购系统、一元云购系统接入短信验证及订单通知功能
  15. php session fixation,什么是session fixation攻击
  16. mysql断开连接_MYSQL,使用什么命令可以断开所有用户的表连接?
  17. Mysql int(10) int(11) 字符长度的设置
  18. 详细的计算机存储层次结构图,必知必会-存储器层次结构
  19. 可乐瓶游戏c语言,中班协调性体育游戏—玩可乐瓶
  20. python实现图片文字提取,有疑问未解决

热门文章

  1. OpenAI Codex
  2. OpenAi使用心得(注册+聊天+CodeX+图片+对接api)
  3. 果客邦水果一件代发货源平台构建“果品零级渠道”供应链新模式
  4. Python 十六进制GBK转中文
  5. windows下批处理设置U盘盘符为U【非PE】
  6. 为什么要解散matlab爱好者微信群?
  7. 笔记本电脑不能连热点和WLAN也能上网、截图
  8. 弹性IP(EIP)不够用的解决办法
  9. 美容院如何设计管理会员卡?
  10. nginx send_timeout 超时导致的302 错误