简体   繁体   中英

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.

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

Response 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

You can set "scale" value of amount as 2.

Try something like this;

instead of responsedto.setAmount(amount) use responsedto.setAmount(amount.setScale(2))

With this you don't need to use @JsonFormat annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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