Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将不带小数部分的BigDecimal表示为整数_Java_Bigdecimal - Fatal编程技术网

Java 将不带小数部分的BigDecimal表示为整数

Java 将不带小数部分的BigDecimal表示为整数,java,bigdecimal,Java,Bigdecimal,如果没有小数部分,我必须将BigDecimal值显示为整数 我的解决方案通常有效,但问题是当bigDecResult收到 在点后有几个零的值,如0.0000 BigDecimal bigDecResult = new BigDecimal("0.0000"); if (bigDecResult.remainder(BigDecimal.ONE).equals(0)) { System.out.println(Integer.toStrin

如果没有小数部分,我必须将
BigDecimal
值显示为整数

我的解决方案通常有效,但问题是当
bigDecResult
收到 在点后有几个零的值,如
0.0000

        BigDecimal bigDecResult = new BigDecimal("0.0000");

        if (bigDecResult.remainder(BigDecimal.ONE).equals(0)) {
            System.out.println(Integer.toString(bigDecResult.intValue()));

        } else {
            System.out.println(bigDecResult.toString());
        }

如果出现
bigDecResult=new BigDecimal(“0.0000”)
,我如何接收
0

什么版本的Java?有
.stripTrailingZeros()
,不幸的是(幸运的是,它在Java8中是固定的),但我需要通用的解决方案。我事先不知道该值是0.000、0.000000001还是123123.000002,整数值只能在第一种情况下使用谢谢你,伙计。很好的解决方案。在我的例子中,我刚刚添加了}else{System.out.println(bd.toString());}
    BigDecimal bd = new BigDecimal("0.0000");

    if (bd.doubleValue() % 1 == 0.0 ){
    System.out.println(bd.setScale(0));

// You can also convert to Integer: 
// Integer response = bd.setScale(0).toBigInteger().intValue();

    } else {
    System.out.println(bd.setScale(4));
    }