Sophia
Sophia

Reputation: 43

round off double value in spring boot

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

Answers (1)

Swapnil Khante
Swapnil Khante

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;
    }
}

Check this out

Upvotes: 4

Related Questions