python - 如何查看数字是否以 .0 结尾

标签 python floating-point integer decimal rounding

如果数字以 .0 结尾,我正在尝试运行测试

我正在运行一个程序,其中的数字相差几个数量级,所以我无法估计到一定数量的数字。使用 % 也不起作用,因为某些数字被排除在外。这个程序中的所有数字都是 float ,所以我需要一种方法来检查它是否以 .0 结尾,而不是以 .00000000000001232 或它必须完全以 .0 结尾的东西

round 函数的问题是我要处理几个数量级的数字。我需要一些东西来检查它在 . 之后是否只有一位小数。或者检查小数点是否为 0 的东西。

代码:

from myro import *
from math import *


def main():

    z = 3
    a = 2
    b = 2
    x = 3
    y = 2 #starts at y = 3

    lim = 25

    c = (a**x + b**y)**(1.0/z)

    resultnum = 0    

    while z <= lim:

        while a <= lim:

            while b <= lim:

                while x <= lim:

                    while y <= lim:

                        y = y + 1
                        c = (a**x + b**y)**(1.0/z)

                        if float(int(c) + 1) != round(c, 6):
                            pass
                        else:
                            print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(int(c)+1) + "^" + str(z)
                            resultnum = resultnum + 1
                            print c

                    y = 3
                    x = x + 1

                x = 3
                b = b + 1

            b = 3
            a = a + 1

        a = 3
        z = z + 1
        print z

    print "code cycle complete"
    print str(resultnum) + " results"


main()

最佳答案

>>> n1 = 2.0
>>> int(n1) == n1 and isinstance(n1, float)
True
>>> n1 = 2
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 2.01
>>> int(n1) == n1 and isinstance(n1, float)
False
>>> n1 = 1E1         #works for scientific notation as well
>>> int(n1) == n1 and isinstance(n1, float)
True

关于python - 如何查看数字是否以 .0 结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16995249/

相关文章:

python - 使用 Pillow 将 png 转换为 jpeg

c# - 是否有一个任务调度程序非常适合在具有超线程的处理器上进行浮点计算?

algorithm - 用整数算法计算 N 次方根

python - 访问 Python 中的实例名称以进行打印

python - 将制定良好的标准作为 Python 中的第一类对象传递给内部 np.where() 调用?

c++ - 我试图将 float 四舍五入到小数点后两位,但这是不正确的。如何在 C++ 中修复此舍入错误?

c++ - 为什么在 C++17 中使用十六进制浮点常量?

ios - 在 Swift 中使用带整数的枚举

lua - 一个数中的质数之和 - Lua

javascript - 如何根据具有不同前缀的其他字符串重命名字符串?