Okay.
So in this case, what I did is I said that's fine.
But in the output it still uses comma as a decimal point.
Coming back to our problem where the input was now accepted with the dot, but the output was still
using a comma.
So it says if your output uses a comma you can use Console.WriteLine culture info invariant culture
as well.
So that means that inside of my console right line statement, I need to use that now for every single
number while converting it into a string.
So that looks looks awfully complicated, but let's use it for once and see how it goes.
Therefore, I'm going back to my code.
Here I'm using dot and to string culture info invariant culture.
So what does this two string do?
Well the thing is we have this variable my number which is of type double.
And as we saw here with double, it has this thing called parse.
So after the dot we can use different methods.
So I can use double dot.
And you see there are a bunch of different things that I can do with this double.
Some of them are methods, some of them are something else.
But I can use those in order to modify whatever comes inside of the brackets.
So in this case, what I'm saying is convert the double into a string and while doing so convert it
using this invariant culture, which basically just means use the dot as a decimal point instead of
the comma.
That is the default in your culture.
So this is something that might not be relevant for you if you are sitting in the US and you're trying
to do that, but just so you're aware that this exists.
So if I do this just for number one, and now I'm going to do the same thing for my other parts of my
code.
And I entered or used the enter here enter key.
Now I'm using concatenation but I'm just doing that for visibility or readability.
So you see it added this plus and then again a dollar for the new string.
So let me go back I'm here just before the end.
And I'm pressing enter.
And it goes into the next line.
And in order to make sure that it stays one statement.
So while it does look like two lines internally, it is considered like one statement and one line,
so to speak.
So now I'm going to do the same thing for my number two and for my sum.
So now let me run this again with my new invariant culture.
So 15.3 and 13.9.
And I get the result of 15.3, 13.9 is 29.2 and a bunch of zeros and three.
So let's take that again and say why that is.
So when adding 15.3 and 13.9 I'm getting the result of there we are 29.2.
Why is that and how can I fix it?
Okay.
So basically what I want to teach you is to use the assistant that is made available to you.
And by the way, you could also use ChatGPT 3.5.
It will do the same thing, at least for the basics that we're using here right now.
So it says the behavior you're observing is due to the way floating point numbers are represented in
computer memory.
Computers represent floating point numbers like doubles in C sharp in binary format.
So in ones and zeros, which can lead to precision issues with certain decimal numbers.
Not all decimal numbers can be represented exactly in binary form, leading to small rounding errors.
This is why you are seeing 29.2 a bunch of zeros and three instead of 29.2.
So how to fix it to address this issue, especially when dealing with financial calculators or when
you need to display results to users, you can round the numbers to a certain number of decimal places.
C-sharp provides methods like math dot round.
For this purpose, you can decide how many decimal places are appropriate for your application.
So it says here the sum would be math dot round sum two.
So this will round the sum to two decimal places.
So then it gives me the code or gave me the code here as well.
So let's go ahead and use just that.
So here we have our sum.
So we're going to use sum is equal to math dot round sum.
And then we can add how many decimal points we want to have.
So here comma two.
So now we're running rounding it to two decimal points.
So we're basically saying whatever was inside of sum overwrite it with whatever this will give us as
a result.
So there is this class called math.
And we're going to see what classes are later on and build our own classes with our.
Own methods, but this class math has a method called round, which rounds a double with your decimal
points or amount of decimal points that you are entering here.
So here I'm saying rounded to two decimal points, and this will then store the new value inside of
some.
So let's try this again and use 15.3 again and 13.9.
And now we're getting 29.2.
So now we have our additional or addition calculator that fixes all kinds of problems along the way.
Really really cool.
There is however obviously one big issue still, and that is if we don't enter numbers but strings for
example, then we still get errors.
But that is something we can only fix a lot later once we understand, try, parse and understand error
handling and so forth.
Okay, so that's it for this video.
Congratulations for making it this far.
And see you in the next video.