Python对小数进行除法运算的正确方法示例
作者:junjie 时间:2021-06-19 08:42:18
求一个算式
a=1
b=2
c=3
print c*(a/b)
运行结果总是0,反复检查拆开以后,发现在Python里,整数初整数,只能得出整数。
也就是 a 除 b 这个结果永远是0,只要把a或者b其中一个数改成浮点数即可。
a=1
b=2
c=3
print c*(a/float(b))
print c*(float(a)/b)
这样才能准确算出a除b的正确结果,当然,如果a比b大,并且不需要小数位数部分可以不用float。
如:
a=1
b=2
c=3
print c/a # 3
print c/b # 1
print c/float(b )# 1.5
标签:Python,小数,除法运算
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IE不支持border-spacing的解决办法
2009-04-28 13:11:00
Python中使用scapy模拟数据包实现arp攻击、dns放大攻击例子
2021-12-04 12:15:05
Linux上安装Python的PIL和Pillow库处理图片的实例教程
2021-01-17 15:22:50
Python实现身份证前六位地区码对照表文件
2022-12-25 10:55:16
![](https://img.aspxhome.com/file/2023/7/99657_0s.jpg)
python 等差数列末项计算方式
2022-01-29 10:12:57
python深度学习人工智能BackPropagation链式法则
2023-07-17 12:23:07
![](https://img.aspxhome.com/file/2023/5/102335_0s.jpg)
Python使用enumerate获取迭代元素下标
2023-03-05 01:46:31
利用不同样式改变相同xhtml结构的布局
2008-08-20 18:17:00
python中实现精确的浮点数运算详解
2022-12-27 07:08:27
Oracle 存储过程总结 二、字符串处理相关函数
2009-07-07 10:28:00
PyCharm搭建一劳永逸的开发环境
2022-12-23 20:24:23
![](https://img.aspxhome.com/file/2023/7/107657_0s.jpg)
关于 Flash Banner 设计的建议
2010-02-02 18:19:00
Python容器使用的5个技巧和2个误区总结
2023-04-09 04:37:01
python绘制圆柱体的方法
2022-07-31 02:00:31
![](https://img.aspxhome.com/file/2023/6/96946_0s.jpg)
深入透析样式表滤镜(下)
2011-06-14 09:49:19
python清空命令行方式
2023-12-08 09:50:35
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
2021-01-15 14:45:25
![](https://img.aspxhome.com/file/2023/7/69587_0s.png)
使用tensorflow实现线性回归
2023-08-14 02:33:55
![](https://img.aspxhome.com/file/2023/6/92516_0s.jpg)
我对SQL SERVER 存储过程见解
2009-10-31 18:51:00
如何使用Python自动生成报表并以邮件发送
2021-07-20 08:51:06