Yes, students in this lesson, I would like to draw attention to some operations with integers and
floating point numbers.
Let's start with use this example.
Imagine you want to divide two integers.
Let's imagine you're implementing overapplication and you want to split the right charge between you
and your friends.
If right charge was 20 bucks and there were three of you, each of you should pay for six bucks.
According to the results of our program, probably each of you will be happy because you all together
saved two bucks, but Uber driver will hit you up.
You need to remember that when you are dividing integers, you receive integer and there is no rounding
fractional.
Part of the result is just not taken into account what you can do about it now.
Just change type of the option to double to get floating point number as a result.
You see, I am custom integer to double here and right in the program, we'll run with you more about
expression, type in the separate less than.
Now you see double the result, but still this is something what the end user wouldn't expect and can't
pay exactly this amount of money.
So what to do in this case?
The rule of thumb here is to use big decimal object.
In previous lessons, we learned how to attach source code to eclipse and how to investigate the source
code.
So feel free to open Javadoc for that small type.
To learn more.
The big Desmonte class provides reparations for arithmetic, scale manipulation around in.
Also, this class gives its user complete control of around in behavior, if no round in mode is specified
and the exact result cannot be represented, an exception is thrown.
Let's declare a variable of big decimal type here.
To create object, object that small use, special method value of like this and calling value of method
here of big decimal type.
This message represents gradational architecture, Partan, which is called factory method.
We'll discuss portions in the separate lesson for now.
Just remember how to create an object of that small type.
You can also set scale for this number.
We are having to deal with money here.
So let me set a scale of two.
This is our right feet.
Let's create a new variable which will contain amount of people.
We have three people.
And now let's divide right feet by amount of people without rounding more specified, we'll have arithmetic
exception like this.
Our program wants to tell us that we can't show the exact calculation result.
Let's set up around in put call my hair in mode.
Not.
You can choose out of different rounded modes here, but let's select the one we learned at school.
And now let's bring the result to console.
And runs the program.
And now we see the results, which we were expected to see.
Let's also review operations with double imagine you have some double number three point one, and you
need to subtract from it one point twenty one when you subtract double value from double value.
You will see a result like this.
The funny story that I order a pizza online recently and applied my bonuses as a result, I got no like
this.
I asked them how they would like me to pay this amount of money we love to meet.
But still, as a programmer, you need to know what to do.
In such cases.
There are infinite number of real numbers in any range, but only a finite number of bits to represent
them.
For this reason, a rounding error is expected for doubles and flaws.
The number you get is the closest number possible that can be represented by double in 14 points representation.
Here's where Big Decimal was rounding.
What will help us?
Let's create the objects of big decimal here, do not forget to set the scale for them.
And let's subtract this number from this one, I'm right in the program and here is the result.
Today, you learned how to control precision in arithmetic operations and how to set up around in the
rules.