From the course: Java 11+ Essential Training

Unlock the full course today

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

Manage currency values with BigDecimal

Manage currency values with BigDecimal - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Manage currency values with BigDecimal

- [Instructor] Primitive numeric types are useful for storing single values in memory. But specifically, double and float types aren't always entirely precise. That's because the way they're stored in memory, doesn't always map exactly to the value. You can look at the documentation for the details of this, but it's better just to see an example. I'll go into jshell and I'm going to create a variable named value to have that explicitly type as a primitive double. And I'll assign it a value of .012. Now, I'm going to add that value together three times. And I'm going to create a new double variable that I'll call pSum, I'm using the letter p for primitive, and I'll give it a value of value + value + value. Now before I show you the result, take a guess at what that should be. You might guess it's .036, but instead, you get .036 and a whole bunch of zeroes, followed by a number at the end. So particularly, when you're…

Contents