OK so we discuss the decimal type and you've already seen 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 upfront 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 10 versus
base 2 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 the differences.
But if you do want to learn more about the difference between them there are some good resources online
and highly recommend that you check out some YouTube videos and I would love to link you to a specific
one.
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 is take them as a unit and decimal.
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 if get fixed point and floating point they are different.
That has to do with what I was describing how they're stored and 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're approximate
and decimals are exact sort of 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 to the cost of precision.
So it doesn't mean that your numbers will be wildly inaccurate.
If you're using a float or 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 7 pages.
So if we had something like
this number here we could run into a problem where this one might not reliably be one when you 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 it's even I didn't 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 a 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 and memory.
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 7.
So if you do use one of these it's better to use doubles.
Which brings us to the question what numeric data types 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 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 that in preciseness 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 things right now.
Critical thing isn't all that it's going to have and there is a price which will go with it.
OK.
So then we'll do our insert into Thing is and we'll do what we need to do.
Price values.
And let's start with a simple one.
Let's do eighty eight point four five.
And if we select that out of there
good we get eighty eight point four or five but we'll run into problems if we start making more complex
longer numbers.
So let's do something like 88 seventy seven point forty five 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 we go back to that table that I had says
around seven issues we run into precision problems seven characters run into position problems.
So if we do something a little longer now 8 8 7 7 6 6 5 5 4 4 and we hit enter.
Notice how we 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 tent places not 10 spaces but multiplied by 10 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 used double rather than float because you have
you know 15 characters 15 places of precision rather than 7 you get double precision.
OK.