【java】对Double类型保留2位小数,并去除尾部多余的0

java测试代码:

package com.ruoyi.workhour.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Slf4j
public class DoubleTest {

    @Test
    void test1() {
        var list = new Double[]{2.3612, 1.0, 120.20, 1200.0, 0.0, 1.2365, 1.30000, 1.4530000, 1 / 3.0, 4.0 / 7};
        for (var i : list) {
            log.info("{}", round3(i, 2).doubleValue());
            log.info("{}", round3str(i, 2));
        }
    }

    BigDecimal round3(Double d, int digit) {
        var d2 = BigDecimal.valueOf(d).setScale(digit, RoundingMode.HALF_UP);
        return d2.stripTrailingZeros();
    }

    String round3str(Double d, int digit) {
        return round3(d, digit).toPlainString();
    }
}

运行结果:

D:\Java\jdk21\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\soft\IDEA\lib\idea_rt.jar=49371:D:\soft\IDEA\bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 @C:\Users\Administrator\AppData\Local\Temp\idea_arg_file1176449103 com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.ruoyi.workhour.service.impl.DoubleTest,test1
22:46:07.858 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 2.36
22:46:07.862 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 2.36
22:46:07.862 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 1.0
22:46:07.862 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 1
22:46:07.862 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 120.2
22:46:07.862 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 120.2
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 1200.0
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 1200
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 0.0
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 0
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 1.24
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 1.24
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 1.3
22:46:07.863 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 1.3
22:46:07.864 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 1.45
22:46:07.865 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 1.45
22:46:07.865 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 0.33
22:46:07.865 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 0.33
22:46:07.865 [main] INFO  c.r.w.s.i.DoubleTest - [test1,16] - 0.57
22:46:07.865 [main] INFO  c.r.w.s.i.DoubleTest - [test1,17] - 0.57

Process finished with exit code 0

  

综上,总结出来的方法如下:

    /**
     * 保留X位小数
     *
     * @param num
     * @param digit
     * @return
     */
    public static BigDecimal round(double num, int digit) {
        return BigDecimal.valueOf(num).setScale(digit, RoundingMode.HALF_UP).stripTrailingZeros();
    }

    /**
     * 保留X位小数字符串,并去除尾部多余的0
     *
     * @param num   待操作数
     * @param digit 保留x位
     * @return
     */
    public static String roundStr(Double num, int digit) {
        if (num == null) return "";

        return round(num, digit).toPlainString();
    }

 

posted on 2024-12-13 22:48  清清飞扬  阅读(6)  评论(0编辑  收藏  举报