python报错TypeError: unsupported operand type(s) for -: ‘decimal.Decimal‘ and ‘float‘的解决方法
问题描述
在编写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‘的解决方法相关推荐
- 解决pip安装时出现报错TypeError: unsupported operand type(s) for -=: ‘Retry‘ and ‘int‘
我在Linux下使用pip安装时出现报错: Exception: Traceback (most recent call last):File "/usr/lib/python2.7/dis ...
- 预测数据时数据类型是object导致报错TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘
解决方法 更换数据类型: data:pd.DataFrame = data.astype('int64') # 或是: data:pd.DataFrame = data.astype('float') ...
- 解决pandas(Python)的报错:unsupported operand type(s) for -: ‘datetime.date’ and ‘Timestamp’
在使用Pandas包的时候,遇到时间加减出现的报错 'datetime.date'是datetime的一种时间格式: 'Timestamp'(注意是大写的字母)是Pandas的一种时间格式. 这两个虽 ...
- python报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x97 in position的解决方法
在编写代码时,调用python解释器中的模块时出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0x97 in position 20: ill ...
- opencv小笔记(TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘NoneType‘)
今天在学习OpenCV的算数操作时,进行图像加法时,运行了下列程序 import numpy as np import cv2 as cv import matplotlib.pyplot as pl ...
- 成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list'
成功解决TypeError: unsupported operand type(s) for +: 'dict_items' and 'list' 目录 解决问题 解决思路 解决方法 解决问题 Typ ...
- 成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict'
成功解决TypeError: unsupported operand type(s) for %: 'NoneType' and 'dict' 目录 解决问题 解决思路 解决方法 解决问题 TypeE ...
- apex安装报错:TypeError: unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
参考解决方法:TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' · Issue #990 · NVIDIA/apex ...
- 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'
最新文章
- WordPress网站的问题
- 自编码器(Auto Encoder)原理及其python实现
- 在C#程序中实现插件架构
- C#如何使用DirectX实现视频播放
- 为input输入框添加圆角并去除阴影
- 实验15 安装新的int 9中断例程
- 如何在读研读博的道路上迅速失败?
- 2 字符串求交集_PostGIS教程十八:维数扩展的9交集模型
- 在线Excel文件解析转换成JSON格式
- TensorFlow精进之路(八):神经元
- kindle看pdf不清楚_Kindle 对 PDF 的支持真的很糟糕吗?
- 自学web前端课程大纲分享,适合所有人学习
- 扫读笔好还是点读笔好 有什么区别
- 云购系统、一元云购系统接入短信验证及订单通知功能
- php session fixation,什么是session fixation攻击
- mysql断开连接_MYSQL,使用什么命令可以断开所有用户的表连接?
- Mysql int(10) int(11) 字符长度的设置
- 详细的计算机存储层次结构图,必知必会-存储器层次结构
- 可乐瓶游戏c语言,中班协调性体育游戏—玩可乐瓶
- python实现图片文字提取,有疑问未解决