Number Types: Floating-point Types
- Rounding errors occur when an exact conversion between numbers is
not possible
double f = 4.35;
System.out.println(100 * f); // prints 434.99999999999994
- Java: Illegal to assign a floating-point expression to an integer
variable
double balance = 13.75;
int dollars = balance; // Error
- Casts: used to convert a value to a different type
int dollars = (int) balance; // OK
Cast discards fractional part.
- Math.round converts a floating-point number to nearest
integer
long rounded = Math.round(balance); // if balance is 13.75, then
//
rounded is set to 14