Welcome back.
In this video, I want to show you how we can extend our solution here, our little application, so
that it can not only add whole numbers but also floating point numbers.
So if you want to have numbers with a decimal point, then you need to use a different data type.
Because int or integers they can only hold whole numbers.
So let's look at how we can modify our program.
So we have my number here which is of type int.
There is this other type.
So let's just try it out.
Let's see what Visual Studio has to offer if we want to use a decimal number.
So we get an error here.
So instead of using zero I'm going to use 0.0 which effectively is the same thing.
But at the same time it isn't.
So if you hover over it you see represents a double precision floating point number.
And then the error that it gives me is cannot implicitly convert type double to int, an explicit conversion
exists.
Are you missing a cast?
So basically it says you could cast from one data type into another.
However I don't want to do that.
What I want to do is instead of using an int here I'm going to use a double.
So I'm going to use my number as a double.
And then I'm going to do the same thing here with my second number.
So my number and let's call it my number one.
And my number two.
They're going to both be doubles.
So you see I can enter zero without the decimal point into a double data type.
But I cannot do it the other way around.
So I cannot use a decimal value for an integer data type.
So for doubles both works, it internally is automatically converting it into 0.0.
So we don't need to manually do that.
However if you enter it it's just a cleaner way of doing it.
Now instead of saying enter a whole number, we're going to say enter a number, because now even a
decimal point number would work and then we are using our user input.
That's fine.
However, now instead of using int parse user input, we're going to use a double parse, because double
also has this parse method that converts a string into a double.
Now obviously we need to use double the number one variable because that's the name of the variable.
We changed the name and I need to do the same here for the sum okay.
Then let's look at the second question here.
So we're asking the user again to enter a number.
And I'm going to say enter a number not a whole number.
We get the user input once again.
And now we need to make the same change here.
What we're going to say double dot parse.
You see we didn't get an error here.
That would have been fine.
The problem would have been that still it could not work with doubles.
Even though we can store an integer inside of a double, but we cannot store a double inside of a of
an integer because an integer, let's say of five, is automatically converted into 5.0, and then it's
stored as a 5.0 double data type in C sharp.
So that's something to consider.
So I'm going to use double parse here as well.
I'm parsing the user input once again.
And now I am adding number one plus number two and storing it inside of sum.
But I get an error here.
It says double number one cannot implicitly convert type double to int okay.
So implicitly means automatically so to speak okay.
So it can implicitly convert int into double but not double into int.
So we can see that.
Let me quickly show you if I have double my sum equals and here int my int one one equals one and int
my int two equals two I can use my int plus one plus my int two.
That will work because it implicitly converts from int into double.
But it cannot implicitly convert from double into int.
So how do we go about it.
How do we solve this problem?
We solve it by making this a double.
So now we're saying double sum is double one plus double two, which basically means my number one plus
my number two.
Now in the output here at the bottom we need to use my number one because the My number variable doesn't
exist anymore.
I made a little change to the naming here.
So let's run this.
So I'm going to use 15.3 and 13.9 for example.
And you see the result of 153 and 100.
Nine is 292.
So that's not correct.
Right.
We should have decimal points in here.
Let me show you why that is.
That has to do with my settings being German.
So I believe my windows is German.
And in Germany the decimal point is a comma.
So we don't use the point as a decimal point.
We use the comma as a decimal point.
So instead of 15.3 we're saying 15 point comma three.
So let me show you what I mean with this.
So let's do the same thing again 15 comma three and 13 comma 9999.
And then we get this here.
So you see the result of 15 comma three and 13 comma nine is 29.2 and a bunch of zeros and some and
a three.
So why is, why that is we're going to look at in a second.
But uh, why we well how we fix the comma and point situation.
That's something we can fix in the next video.
So see you there.