如何测试一个数字是否可以被小于1的十进制整除? (54.10%.10) [英] how to test if a number is divisible by a decimal less than 1? (54.10 % .10)

查看:91
本文介绍了如何测试一个数字是否可以被小于1的十进制整除? (54.10%.10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试 float ie( 54.10 )是否可被 0.10 。 54.10%.10 返回 .10 而不是 0 ,为什么?为什么要让它做我想做的事情?

I'm trying to test if a float i.e(54.10) is divisible by 0.10. 54.10 % .10 returns .10 and not 0, why is that and how can I get it to do what I want it to do?

推荐答案

您可以使用 十进制 模块以避免浮点精度问题:

You can use the decimal module to avoid the floating point precision problem:

>>> import decimal
>>> decimal.Decimal('54.10') % decimal.Decimal('0.10')
Decimal('0.00')

这篇关于如何测试一个数字是否可以被小于1的十进制整除? (54.10%.10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆