From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Representing currency values with BigDecimal

Representing currency values with BigDecimal - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Representing currency values with BigDecimal

- Primitive numeric types are incredibly useful for storing single values in memory. But specifically double and float types aren't entirely precise. That's because the way they're stored in memory doesn't map exactly to the value. For example, a double value is supposed to take 64 bits but Java doesn't use all 64 bits, it only stores what it considers the important parts of the number. So you can see some pretty odd things when you start adding values together and then outputting the result. In this project, Currency, I've opened the main class which has a declaration of a double value with a value of .012. I'm going to declare a second double value that I'll name pSum, for primitive sum. And I'll calculate it by adding value together three times, with value + value + value. Now, that's pretty easy to figure out, three times .012 should be .036. But now I'll output the result. I'll use sout again and expand that to System.out.println then I'll put in a label of "Sum of primitives: "…

Contents