So we discussed the decimal type and you've already seen the end type, but we're focusing on the decimal
type or we did focus on it.
But like I said, there's a bit of competition.
So there are other types that we could use to represent similar data, which is to say numbers that
are not whole numbers.
So there's also things like float and double.
So we're going to talk about those in this video.
The differences have to be up front between all of them are quite technical.
They have to do with how things are stored in memory.
It would be difficult to explain without going all the way into how binary works and base ten versus
base two and how things are stored in memory.
So I'm going to kind of skim over some of this because the honest answer is that it doesn't or the truth
is that it doesn't really matter.
All that matters is that, you know, functionally what the differences.
But if you do want to learn more about the difference between them, there are some good resources online
and I highly recommend that you check out some YouTube videos and I would love to link you to a specific
one.
However, I've had some copyright claim issues, so last time I did that, I'm not going to do it.
Now, if you're curious, you can research it.
But essentially, like I said, it boils down to how they're stored.
So the big question is what is the difference between float and double?
Let's take them as a unit and decimal.
And if we go to the documentation, there's a page that lays it out relatively plainly.
It says the decimal data type is a fixed point type and calculations are exact.
The float and double data types are floating point types and calculations are approximate.
So we've got fixed point and floating point.
They are different.
That's that has to do with how what I was describing how they're stored in memory is different.
That's not as important.
Just knowing that they are different is important.
However, the side effect of how they're stored is that float and doubles are approximate, which seems
kind of weird.
It doesn't seem like you would ever want to use them if they are not exact, but essentially they approximate
and decimals are exact.
So to boil it down, float and double will store larger numbers using less space.
So that's an advantage.
They can take gigantic numbers, things that you can't store in a decimal.
And they'll use less space.
However, that comes at a big cost.
It's a big but.
It comes with the cost of precision.
So it doesn't mean that your numbers will be wildly inaccurate if you're using a float or a double.
But it does mean that they're not going to be as accurate as a decimal.
So here's a simple table that it just basically takes information from the documentation floats will
have precision issues after around seven digits.
So if we had something like.
This number here, we could run into a problem where this one might not reliably be one.
When we retrieve it, it might change and that could be problematic if you need it to be precise.
But if you're working with gigantic numbers, something like this or let's even add in something like
this, this one at the very end isn't going to be a deal breaker most of the time, depending on what
you're working with.
With that said, it's really important to know that there is difference and there's a lot of kind of
classic bugs and issues.
People have run into lots of blog posts around floating point math problems, floating point math issues
and bugs.
Doubles are basically the same thing as a float.
They're just bigger.
They have larger capacity.
However, they take up more space in memory.
As you can see, four bytes versus eight bytes.
A double, however, is more precise.
So it's precise to roughly 15 digits rather than just seven.
So if you do use one of these, it's better to use double.
Which brings us to the question what numeric data type should you use when you're storing information
that has a decimal?
And the answer, at least in my opinion, is you should always try and use decimal unless you know that
precision doesn't matter.
So if you're doing things like financial data prices, if you're a bank, you need that precision.
Absolutely.
You need to use decimal.
Otherwise you can get away by with floats or doubles.
But for the most part I'll be using decimal.
It's kind of my preferred data type when we're working with decimals.
Oh, and before I forget.
Well, actually, I already forgot, but this is after I recorded the video.
I'm going and adding this in.
I wanted to show you that you can actually see the inconsistency, the imprecise ness of using floats
and doubles as well.
So I'm going to create a simple table and I will just call it Let's go with thingies.
Right on.
Great table thing isn't all that is going to happen.
There is a price which will go with float.
So then we'll do our insert into thingies and we'll do what we need to do.
Price values.
And let's start with a simple one.
Let's do 88.45 and if we select that out of there,
good, we get 88.45, but we'll run into problems if we start making more complex, longer numbers.
So let's do something like 88, 77.45, and that should still work.
If we select it out, you can see it's unchanged.
Remember, we're working with four, five, six characters here.
And if we go back to that table that I had says around seven issues, we run into precision problems,
seven characters run into precision problems.
So if we do something a little longer now eight, eight, seven, seven, six, six, five, five,
four, four and we hit enter notice how it got stored.
So it doesn't just truncate, it doesn't chop things off, but it only stores the first seven digits
and then it just puts these empty tens places.
Well, not tens places, but it multiplies by ten add zeros essentially at the end.
So we end up with this, which is very different than this.
And if we instead had this as a double instead of a float, we would have that precision extended out
to 15 characters.
So it is significant and it makes a big impact depending on what you're doing and what you need.
So that's why I recommend using decimal, where you can control exactly what you need and how long you
need things to be.
However, if you're working with gigantic numbers and you don't need to be that precise, you can use
double or float.
But typically my rule of thumb is if you need to use one, use double rather than float because you
have 15 characters, 15 places of precision rather than seven.
You get double the precision.