Reputation: 43
Is there any spring boot attribute to round off double value
For example
@roundOfDouble(Any Parameter ?)
Double amount; 1500.95 -> 1501.00
If null, then 0.00
any suggestions ?
Upvotes: 0
Views: 1188
Reputation: 659
This is a general problem with doubles
and if you have such requirement then double
should be avoided. Maybe switching to BigDecimal
could help your use case. It will certainly give you flexibility to put some combination to round off over doubles.
For example
public class Invoice {
@DecimalMin(value = "0.0", inclusive = false)
@Digits(integer=3, fraction=2)
private BigDecimal price;
private String description;
public Invoice(BigDecimal price, String description) {
this.price = price;
this.description = description;
}
}
Upvotes: 4