随笔 - 493  文章 - 0  评论 - 97  阅读 - 239万

【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();
    }
}
复制代码

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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   清清飞扬  阅读(105)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示