Concurrent Work

Rambling of a coder who craves for knowledge and understanding of the world.

Written by Dongfang Qu

25 Apr 2017

api接口中设置double浮点类数的最大小数位

java开发的restful api中,偶尔需要将最大有效位限制在一个固定的范围内,如:最多保留小数点后4位。 一番dig后,整理一个简单方案记录如下:

SerializeConfig.getGlobalInstance().put(Double.class, new DoubleSerializer(new DecimalFormat("#.####")));
// 测试用例
    @Test
    public void floatDataFmtTest() {
        Double[] data = new Double[]{123456789d, 1d, 1234d, 12345d, 0.1d, 0.1234d, 0.12345d};

        DecimalFormat df = new DecimalFormat("#.####");
        for (Double v : data) {
            System.out.println(df.format(v));
        }
    }
# 输出结果
123456789
1
1234
12345
0.1
0.1234
0.1235

Categories