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 - Java Tutorial
From the course: Java 8 Essential Training
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: "…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Working with primitive variables8m 4s
-
Declaring and initializing numeric primitives6m 14s
-
Representing currency values with BigDecimal5m 10s
-
Converting numeric values5m 37s
-
Using mathematical operators and the Math class6m 1s
-
Working with Boolean values and expressions4m 49s
-
Working with character values3m 23s
-
Using Java operators6m 11s
-
-
-
-
-
-
-
-
-
-