下面是我的示例域对象,它通过'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
}
提前致谢