Spring的产生=“application / json”没有维持双重值的比例作为响应

时间:2016-03-09 05:16:29

标签: json spring

下面是我的示例域对象,它通过'produce =“application / json”'

自动转换为json
    public class Total {

        private double amount;

        public double getAmount() {
            return amount;
        }

        public void setAmount(double amount) {
            this.amount= amount;
        }
    }

以下是我的示例资源,它从域对象

生成json
    @RequestMapping(value = "/test", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Total get(@RequestBody RequestDomain reqDomain) {

            Total total = new Total();
            // Here I am getting some amount in String format e.g 2.5000
            // I want to round that value to 2 decimal values

            String inputValue = "2.5000"; // Sample value(I am getting this from somewhere)

            // Rounding this value to 2 decimal places
            BigDecimal bigDec = new BigDecimal(inputValue);
            bigDec = bigDec.setScale(2, BigDecimal.ROUND_HALF_EVEN);

            // Setting the rounded value to domain object
            total.setAmount(bigDec.doubleValue());          
            return total;

        }

正在创建的json如下

    {
        "amount" : 2.5
    }

我希望输出只有两位小数

    {
        "amount" : 2.50
    }

提前致谢

0 个答案:

没有答案