You can use the double tape to store decimal numbers.
So far, we used the end long variable to store whole numbers.
I want to emphasize the word hole because you cannot store decimal values with Ent. or along.
The entire variable is used to store small hole numbers and the long variable can store big hole numbers.
So in this video, we're going to use the double tape to store decimal numbers.
First thing I'll need you to do is create a new class by yourself inside the section to project create
a new file named Decimals Java and inside the decimals class.
Make sure it has the main method.
You can store decimals in a double variable, you can't use Int or long to store decimals, although
we can do so in a double.
And there are three things you need to right to define a double.
You have to specify the double type, which says that this variable is meant to store decimals, the
variable name, which in this case is price and the decimal value that you want to store.
This code creates a variable called price, which stores a decimal value of three point ninety nine.
So when the decimals class, we're going to create a variable named percentage double.
Percentage.
Is equal to fifty five point seven.
Now we're going to print this percentage inside a string system, dart out dot print line will say in
2020.
Disconnect the string, embed, the percentage will say in 2020, some percentage.
Of the world's population is urban.
OK, compile your code and run it.
Looks good.
The variable percentage stores the decimal fifty five point seven, and you printed the value from within
a string.
Easy stuff.
Back to our quote, I'd like to emphasize that you cannot store in decimals inside an Int or long variable.
If you try to do it, you'll get an error.
Let's try to change percentage to Annette.
Compile your code.
And we get an error in this line of our code and variables can only store whole numbers, so change
the variable back to a double.
And clear the output.
Now, what happens if we reverse this and try to store a whole number in a double variable?
Let's find out double.
Dividend, we're going to create a double variable dividend and set it equal to 25.
And we'll print it system dot out, dot print line dividend.
Compiling our code and running it.
And it prints it as a decimal double variables are special because even if you don't express the number
as a decimal double, it's going to store it as one.
In this case, the dividend variable stores the value twenty five.
But since dividend is a double, it stores it as a decimal 25 point zero.
With that being said, it's imperative that you always use double for math calculations if precision
is important.
You need to have decimals.
So create a variable named deviser double divisor.
And set it equal to two.
Then we can divide both numbers using the division symbol, the front slash, so inside the Prince Mint
will print the result of dividing both numbers.
And as expected, 25 divided by two is twelve point five and dividend is a double edged sword.
25 as a decimal, 25 point zero, the divisor double variable stores, two as a decimal 2.0.
And when you divide a decimal by another, the result is a decimal value.
All right, with that being said, avoid using int or long variables for math calculations.
If you divide two whole numbers, the result will always be a whole number.
This may not seem like an issue, but make dividend and deviser integer variables instead into dividend
and divisor.
Now, run that.
And I hope you can see that the result is not correct, 25 divided by two is twelve point five, not
12.
The dividend variable stores, a whole number 25, the divisor variable also stores a whole number,
and if you divide two integers, the result is going to be an integer.
Int isn't able to work with decimals.
So what it does is it cuts off decimals from the results and keeps only the integer in math.
You need decimals to stay accurate.
So always use double variables in math operations because double treats every number as a decimal and
keeps the math accurate.
So going to switch this back to double's.
All right, finally, we could make the print statements more interesting to end off on a high note.
So what we're going to do is embed this result in a string as well.
We're going to print the dividend disvalue.
Divided by break the string.
The divisor value.
Is equal to.
The arithmetic result of dividing dividend by divisor.
Run the code.
OK, let's add some space between the word dividend and the first string recompiling the code.
We can wrap this lesson up with these rules, use into historical numbers, use long to store very large
hole numbers and use double to store and work with decimals.
And this lesson you learn to use double to sword decimal values, you created a double variable that
stores a percentage and then you printed the value for more than a string.
That was pretty easy.
And double stories, any number as a decimal, even if you don't express it as one and decimals help
keep the math accurate, that's why you should always use double for math, avoid using whole numbers
in math calculations, because if you divide two integer values, the result is going to be an integer
value.
It cuts off the decimal and keeps only the integer, which is not correct.
So to avoid mistakes, no to use int to store whole numbers long, to store very large hole numbers
and double to store and work with decimals.