June 202307:5245.5k views

Storing money in MySQL (the right way)

About

My one hard line: never store money in a FLOAT or a DOUBLE. They're approximations, and I show a sum coming out a fraction of a penny off. Use DECIMAL, or store cents in an integer like Stripe does.

Transcript

Everything we said

00:00

Aaron Francis

I try not to have a lot of hot takes. I try not to draw a lot of hard lines. I like to lay out the available options and let you, the very, very smart people of YouTube, decide what's best for you. I understand that's not how YouTube works. I'm supposed to have a hot take. I'm supposed to draw a hard line, and so here I am. I'm going to draw a hard line. If you are storing monetary values in MySQL, you should never, ever, ever use the float or the double.

00:31

Don't use the float column. Don't use the double column. You should-- I'm gonna give you an option here. You should use the decimal column or maybe one of the integer columns. The reason is the float and the double columns are floating point. They are approximations. They're very good approximations, but they're not perfectly accurate, and when it comes to dealing with financial data, you want perfect accuracy.

01:00

You don't want an approximation, no matter how good that approximation is. When you go to add up two numbers, you want, you want the sum to be correct, not pretty correct. Let me show you what I'm talking about. I'm gonna create a table here called money, and I'm gonna create two columns: money one and money two, and those are both going to be double.

01:20

Remember, don't do this. This is the example of what can go wrong. So if I create this, that looks like it was created just fine. Now I'm going to insert some data. So I'm gonna insert this data in there. Now, if we take a look at this, I'm inserting into money one and money two. I'm inserting the value of one hundred point four and negative eighty, inserting that into column money one, and I'm inserting twenty point four and zero into money two.

01:50

If we run that, and then we read it back, select star from money. If we take a look at that, and we use our very powerful brains, and we look at that, and we say, "Hmm, I wonder what those rows sum to." Well, they would both sum to twenty point four, right? So a hundred point four less eighty is twenty point four, and twenty point four plus zero is twenty point four.

02:16

That makes sense to me. Is that what is going to happen? Let's find out together. We're gonna select the sum of money one and the sum of money two from the table. Uh-oh. It didn't sum out to the same thing. It's very close.

02:34

Unidentified

I'm just talking about fractions of a penny here, okay?

02:38

Aaron Francis

So close, but they don't equal each other. Importantly, if we were to put the equal sign in there, MySQL says, "Nah, those aren't the same thing." And to be fair, i- it's correct. They're not the same thing. One is twenty point four plus a little bit, and one is twenty point four. So this is what happens when you store monetary values in the wrong type of column.

03:02

Let's run this entire thing back. Let's drop table money. So we're gonna drop this table altogether, and instead of doing doubles, what we're gonna do is decimals. So we're gonna do decimals, and with a decimal, you have to specify, um, you have to specify how big the column can be. So we're gonna say it can be ten digits wide, so it can hold a total of ten digits, and of those ten digits, we're gonna allocate two of those digits to be after

03:32

the decimal. So we're gonna have eight before the decimal and two after the decimal. So we're gonna do that in both places, and if we run that, we've got a new table now. Now we've got some values. Let's read them back. It looks like the same values as before, but you do see we have that trailing zero on both of them because we specified that there are two digits after the decimal place, right?

03:56

Now, if we run our sum, look at that. It is perfectly exactly equal because the decimal column is not an approximation. It is an exact representation of the number that you put in, which is why you wanna use it for monetary values. Because I am so allergic to hot takes and hard lines, I'm gonna give you an option.

04:18

I have shown you why, I hope, why you should use decimal over float or double. Because float and double are approximations and decimal is exact. There is another keyword in MySQL called numeric, which is just an alias for decimal. So you can use numeric if you want. That's not really the option I was gonna give you. The option that I'm going to give you is you could use any of the integer type columns.

04:44

So integer columns are exact representations, nothing approximate about it. So you could use a s-- you could use a small int if it's really small. You could use a big int if it's really big. But what you could do is instead of storing a fractional, um, currency, so let's say you're storing US dollars. Instead of storing the dollars and the cents, what you would do is you would multiply it by a hundred so that you're storing the cents only.

05:09

Let's drop this table, and we're gonna recreate it with integers. So there are a whole bunch of different types of integers, and they all vary based on the range of data that they can store. For this example, integer is just fine. Now, instead of inserting a hundred point four, we have to ensure that there are no fractional parts remaining. So what we're doing is we're taking it out of the, um, the denomination that it's in, and we're moving it into the lowest possible denomination so that there are no more fractional parts.

05:40

In the case of the US dollar, that would be taking out of, um, a hundred dollars and turning it into a number of cents. This is, um, this is what Stripe does. Stripe will return all the transactions in the lowest possible denomination of a currency. And so this is a totally viable method. You just have a little bit, you have a little bit of overhead, usually on the application side, not even, not even in like a, a processing sense, just in, in literal mental overhead.

06:09

You have to remember everything is stored in cents, not in fractional dollars. So what we would do here is every time we insert something, we would insert it times a hundred such that we move that decimal place over two times. And then if we read it back, it looks the same but a little bit different. So instead of saying there's a hundred point four dollars in here, what we're saying is there's ten thousand and forty cents in here.

06:33

Same thing, just different. Now, if we sum this up and read it back, you'll see that they're exactly the same because integers are exact. So there is absolutely no approximation going on with an i-- with an integer. So we can sum up as many as we want, and it will work exactly as we had hoped, just like it does with the decimals. So which one of these you choose is totally up to you.

06:56

I can't, I can't bring myself to make the choice for you. I've never seen your application. I don't know what your data is doing. I have no idea what's going on. So I am allergic to telling you exactly what to do. But if it'll make you feel better, I will tell you kind of exactly what to u-- what to do. Um, use a decimal or an integer. Don't use a float or a double.

07:17

A float and double are approximations, and they will get you in trouble when it comes time to add them up or do basically any math on them. Using a decimal or an integer are exact, and that is what you want when you're dealing with financial data. If you like this, we've got tons of videos at planetscale.com/mysql.

07:38

I teach an entire course on MySQL. Um, if you don't want to go there, just stay tuned to this channel. We've got lots of MySQL videos here as well. See ya.

Databases

More videos on databases

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues