BigDecimal double float

double float

The data types double and float are primitive types. The difference to classes and wrapper is the better performance, because no object needs to be created with new.

This data types can not be used for business application development, because they can not store decimal numbers exactly.

double d = 1234.56; BigDecimal number = new BigDecimal(d); System.out.println(number.toString());
Result: 1234.55999999999994543031789362430572509765625


In contrast to double and float the class BigDecimal can store decimal numbers with arbitrary decimal places exactly and has futhermore some functions for exact calculations. Decimal numbers like price or percent will mainly be coded with BigDecimal.

But you should care about correct initialization of BigDecimal with the string constructor, to ensure that the number will be stored with exact decimal places.

BigDecimal number = new BigDecimal("1234.56"); System.out.println(number.toString());
Result: 1234.56

Johannes Hammoud BigDecimal double float 22.10.2007

I Coding : Community about Java programing

Language German+-

Java JSF JavaScript HTML CSS NetBeans GlassFish MySQL