首页 > 解决方案 > 将 double 转换为 int 返回 0 而不是 1 为什么?

问题描述

答案是 0 而不是 1,为什么

public class Student {

    public static void main(String[] args) {

        double s=1.3901863961920014E-5;
        int a= (int)s;
        System.out.println("a: "+a);
    }
}

标签: javanumbers

解决方案


因为你的号码

1.3901863961920014E-5

是 :

0.000013901863961920014013868597546608185666627832688391208648681640625

您可以尝试像这样检查全长:

double s = 1.3901863961920014E-5;
BigDecimal b = new BigDecimal(s);
System.out.println(b);
=> 0.000013901863961920014013868597546608185666627832688391208648681640625

推荐阅读