WEBVTT

00:00.140 --> 00:06.450
OK so we discuss the decimal type and you've already seen type but we're focusing on the decimal type

00:06.960 --> 00:08.430
or we did focus on it.

00:08.460 --> 00:11.400
But like I said there's a bit of competition.

00:11.640 --> 00:16.950
So there are other types that we could use to represent similar data which is to say numbers that are

00:16.950 --> 00:19.260
not whole numbers.

00:19.310 --> 00:25.560
So there's also things like float and double.

00:25.870 --> 00:27.980
So we're going to talk about those in this video.

00:28.090 --> 00:33.560
The differences have to be upfront between all of them are quite technical.

00:33.610 --> 00:36.940
They have to do with how things are stored in memory.

00:37.180 --> 00:44.410
It would be difficult to explain without going all the way into how binary works and base 10 versus

00:44.410 --> 00:47.300
base 2 and how things are stored in memory.

00:47.350 --> 00:53.190
So I'm going to kind of skim over some of this because the honest answer is that it doesn't.

00:53.260 --> 00:56.230
Or the truth is that it doesn't really matter.

00:56.230 --> 00:58.830
All that matters is that you know functionally the differences.

00:58.840 --> 01:03.660
But if you do want to learn more about the difference between them there are some good resources online

01:03.670 --> 01:08.380
and highly recommend that you check out some YouTube videos and I would love to link you to a specific

01:08.380 --> 01:09.200
one.

01:09.280 --> 01:11.080
I've had some copyright claim issues.

01:11.110 --> 01:13.210
So last time I did that I'm not going to do it.

01:13.210 --> 01:15.730
Now if you're curious you can research it.

01:15.820 --> 01:19.000
But essentially like I said it boils down to how they're stored.

01:19.000 --> 01:26.880
So the big question is what is the difference between float and double is take them as a unit and decimal.

01:27.100 --> 01:33.290
If we go to the documentation there's a page that lays it out relatively plainly.

01:33.390 --> 01:38.840
It says the Decimal data type is a fixed point type and calculations are exact.

01:39.100 --> 01:45.090
The float and double data types are floating point types and calculations are approximate.

01:45.360 --> 01:49.420
So if get fixed point and floating point they are different.

01:49.590 --> 01:54.350
That has to do with what I was describing how they're stored and memory is different.

01:55.290 --> 01:58.640
That's not as important just knowing that they are different is important.

01:58.680 --> 02:05.610
However the side effect of how they're stored is that float and doubles are approximate which seems

02:05.610 --> 02:07.270
kind of weird.

02:07.500 --> 02:14.610
It doesn't seem like you would ever want to use them if they are not exact but essentially they're approximate

02:15.390 --> 02:25.180
and decimals are exact sort of boil it down float and double will store larger numbers using less space

02:25.190 --> 02:26.790
so that's an advantage.

02:27.030 --> 02:33.600
They can take gigantic numbers things that you can't store in a decimal and they'll use less space.

02:33.870 --> 02:35.530
However that comes at a big cost.

02:35.530 --> 02:40.010
It's a big but it comes to the cost of precision.

02:40.540 --> 02:43.890
So it doesn't mean that your numbers will be wildly inaccurate.

02:43.930 --> 02:49.710
If you're using a float or double but it does mean that they're not going to be as accurate as a decimal

02:51.450 --> 02:59.100
So here's a simple table that it just basically takes information from the documentation floats will

02:59.160 --> 03:00.750
have precision issues.

03:00.750 --> 03:03.430
After around 7 pages.

03:03.780 --> 03:05.490
So if we had something like

03:08.770 --> 03:14.980
this number here we could run into a problem where this one might not reliably be one when you retrieve

03:14.980 --> 03:17.110
it it might change.

03:17.380 --> 03:21.780
And that could be problematic if you need it to be precise.

03:22.030 --> 03:27.640
But if you're working with gigantic numbers something like this or it's even I didn't something like

03:27.640 --> 03:29.680
this.

03:29.680 --> 03:33.210
This one at the very end isn't going to be a deal breaker.

03:33.220 --> 03:36.700
Most of the time depending on what you're working with.

03:36.700 --> 03:41.800
With that said it's really important to know that there is a difference and there's a lot of kind of

03:42.130 --> 03:48.540
classic bugs and issues people have run into lots of blog posts around floating point math problems

03:49.180 --> 03:52.630
floating point math issues and bugs.

03:52.630 --> 03:55.410
Doubles are basically the same thing as a float.

03:55.540 --> 03:56.560
They're just bigger.

03:56.560 --> 04:01.400
They have larger capacity however they take up more space and memory.

04:02.080 --> 04:04.530
You can see four bytes versus eight bytes.

04:04.990 --> 04:07.450
A double however is more precise.

04:08.270 --> 04:12.720
So it's precise to roughly 15 digits rather than just 7.

04:13.280 --> 04:17.150
So if you do use one of these it's better to use doubles.

04:17.570 --> 04:24.080
Which brings us to the question what numeric data types should you use when you're storing information

04:24.080 --> 04:25.550
that has a decimal.

04:25.580 --> 04:31.430
And the answer at least in my opinion is you should always try and use decimal unless you know that

04:31.430 --> 04:32.660
precision doesn't matter.

04:32.870 --> 04:36.840
So if you're doing things like financial data prices.

04:36.920 --> 04:40.310
If you're a bank you need that precision Absolutely.

04:40.370 --> 04:45.580
You need to use decimal Otherwise you can get away by with floats or doubles.

04:45.710 --> 04:48.200
But for the most part I'll be using decimal.

04:48.250 --> 04:52.830
It's kind of my preferred data type when we're working with decimals.

04:52.840 --> 04:58.590
Oh and before I forget Actually I already forgot but this is after I recorded the video.

04:58.730 --> 05:00.330
I'm going and adding this in.

05:00.380 --> 05:07.280
I wanted to show you that you can actually see the inconsistency that in preciseness of using floats

05:07.730 --> 05:09.510
and doubles as well.

05:09.530 --> 05:21.570
So I'm going to create a simple table and I will just call it let's go with things right now.

05:21.810 --> 05:30.150
Critical thing isn't all that it's going to have and there is a price which will go with it.

05:30.570 --> 05:31.480
OK.

05:31.800 --> 05:38.670
So then we'll do our insert into Thing is and we'll do what we need to do.

05:38.670 --> 05:42.350
Price values.

05:42.690 --> 05:44.250
And let's start with a simple one.

05:44.360 --> 05:48.660
Let's do eighty eight point four five.

05:49.230 --> 05:50.570
And if we select that out of there

05:54.910 --> 06:02.470
good we get eighty eight point four or five but we'll run into problems if we start making more complex

06:02.470 --> 06:03.490
longer numbers.

06:03.670 --> 06:12.080
So let's do something like 88 seventy seven point forty five and that should still work.

06:12.610 --> 06:15.260
If we select it out you can see it's unchanged.

06:15.430 --> 06:22.050
Remember we're working with four five six characters here and we go back to that table that I had says

06:22.070 --> 06:28.870
around seven issues we run into precision problems seven characters run into position problems.

06:28.870 --> 06:39.880
So if we do something a little longer now 8 8 7 7 6 6 5 5 4 4 and we hit enter.

06:39.880 --> 06:41.950
Notice how we got stored.

06:41.950 --> 06:48.340
So it doesn't just truncate it doesn't chop things off but it only stores the first seven digits.

06:48.940 --> 06:55.480
And then it just puts these empty tent places not 10 spaces but multiplied by 10 zeros essentially at

06:55.480 --> 06:56.250
the end.

06:56.740 --> 07:05.450
So we end up with this which is very different than this and if we instead had this as a double instead

07:05.450 --> 07:10.610
of a float we would have that precision extended out to 15 characters.

07:10.610 --> 07:15.820
So it is significant and it makes a big impact depending on what you're doing and what you need.

07:16.280 --> 07:22.490
So that's why I recommend using decimal where you can control exactly what you need and how long you

07:22.490 --> 07:23.480
need things to be.

07:23.480 --> 07:28.220
However if you're working with gigantic numbers and you don't need to be that precise you can use double

07:29.060 --> 07:29.550
or float.

07:29.630 --> 07:35.960
But typically My rule of thumb is if you need to use one used double rather than float because you have

07:36.620 --> 07:44.320
you know 15 characters 15 places of precision rather than 7 you get double precision.

07:44.570 --> 07:44.790
OK.
