---
title: Storing money in MySQL (the right way)
url: "https://aaronfrancis.com/videos/storing-money-in-mysql-the-right-way"
path: "/videos/storing-money-in-mysql-the-right-way"
type: video
author: Aaron Francis
published: 2023-06-21
topic: Databases
description: "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."
keywords: [mysql money storage, decimal type, floating point precision, storing cents, stripe payment model, financial data types, mysql, database design, stripe]
youtube_url: "https://www.youtube.com/watch?v=fhwEFZ34c7g"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Storing money in MySQL (the right way)

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

**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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

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

**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.

**Aaron Francis:** 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

**Aaron Francis:** 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?

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

---

## Related on aaronfrancis.com

- [Making videos developers actually want to watch](/podcast/making-videos-developers-actually-want-to-watch-6e3ad549) *(Podcast · 2024-06-03 · shared: mysql, database design)*
- [MySQL for application developers](/about/speaking/mysql-for-application-developers) *(Talk · 2023-04-20 · shared: mysql, database design)*
- [MySQL for Developers](/courses) *(Course · 2023-03-01 · shared: mysql, database design)*
- [You don't always need JOINs](/videos/you-dont-always-need-joins) *(Video · 2023-11-22 · shared: mysql, database design)*
- [Storing feature flags as JSON (with indexing)](/videos/storing-feature-flags-as-json-with-indexing) *(Video · 2023-11-14 · shared: mysql, database design)*

## Relevant Resources from Aaron Francis

- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.
- **[Mastering Postgres](https://databaseschool.com)** (overview on site: [/courses](/courses)): Deep-dive video course on PostgreSQL indexing, query performance, JSONB, full-text search, locking, and production operations.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `mysql money storage` ([M-N](/llms/keywords/m-n.txt)), `decimal type` ([D-E](/llms/keywords/d-e.txt)), `floating point precision` ([F-G](/llms/keywords/f-g.txt)), `storing cents` ([S](/llms/keywords/s.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
