liveBook · Manning

BigDecimal

As explained in Spring Start Here, Second Edition

BigDecimal is a Java numeric type that represents decimal values with arbitrary precision and is suitable for exact monetary calculations.

Prices need decimal precision: Java’s double and float can lose precision during arithmetic because of how they store values. Use BigDecimal for sensitive values such as purchase prices. section 12.2

public class Purchase {
 
  private int id;
  private String product;
  private BigDecimal price;
  // Omitted getters and setters
}

The Purchase model declares its price field as BigDecimal; JdbcTemplate can also read a price with getBigDecimal. section 12.2

How Spring Start Here, Second Edition covers it

How deep it goes
Explains precision-loss rationale and price-field use, but does not develop BigDecimal arithmetic.
Examples
Uses a purchase model’s price field and database row mapping as examples.
What you can do after
Apply BigDecimal instead of double or float when modeling prices that need decimal precision.
How it connects
Contrasts BigDecimal with double and float, and uses it in a purchase model with JdbcTemplate.

Also explained in

Compare how 2 books explain BigDecimal →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.