简体   繁体   English

如何在 Java Spring 引导中为 BigDecimal 保留小数点后的零?

[英]How to keep zeros after decimal for BigDecimal in Java Spring Boot?

I have some BigDecimal values in database, like 10000.00, 12357.87 etc.我在数据库中有一些BigDecimal值,例如 10000.00、12357.87 等。

Now I'm trying to get these values by using GET API REST in spring boot, but here the problem is in Swagger or Postman response I see like: Now I'm trying to get these values by using GET API REST in spring boot, but here the problem is in Swagger or Postman response I see like:

"amount":"10000.00"

but I want value without quotes "amount": 10000.00但我想要不带引号的值 "amount": 10000.00

Response class:响应 class:

public class ResponseDTO{
  @JsonFormat(shape = JsonFormat.Shape.STRING)
  private BigDecimal amount;
}

If I removed @JsonFormat(shape = JsonFormat.Shape.STRING) annotation it will shown as "amount":10000如果我删除@JsonFormat(shape = JsonFormat.Shape.STRING)注释,它将显示为"amount":10000

You can set "scale" value of amount as 2.您可以将金额的“比例”值设置为 2。

Try something like this;尝试这样的事情;

instead of responsedto.setAmount(amount) use responsedto.setAmount(amount.setScale(2))而不是responsedto.setAmount(amount)使用responsedto.setAmount(amount.setScale(2))

With this you don't need to use @JsonFormat annotation.有了这个,你不需要使用 @JsonFormat 注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM