[{"data":1,"prerenderedAt":518},["ShallowReactive",2],{"category-data-mysql":3},[4],{"id":5,"title":6,"body":7,"description":505,"extension":506,"meta":507,"navigation":139,"ogImage":509,"path":514,"seo":515,"stem":516,"__hash__":517},"content/blogs/8. float-make-my-dollar-float-away.md","FLOAT Made My Dollars Float Away - FLOAT vs DECIMAL in MySQL 💸",{"type":8,"value":9,"toc":494},"minimark",[10,15,33,44,51,58,62,71,78,92,95,99,158,161,197,207,211,214,229,232,260,263,278,284,291,295,301,318,359,361,385,388,392,395,404,407,419,422,426,434,438,470,474,484,490],[11,12,14],"h3",{"id":13},"recently-i-got-a-task","Recently I got a task:",[16,17,18],"blockquote",{},[19,20,21],"p",{},[22,23,24,25,29,30],"strong",{},"Alter a table column from ",[26,27,28],"code",{},"FLOAT"," to ",[26,31,32],{},"DECIMAL(10,2)",[19,34,35,36],{},"I thought:\n",[37,38,39,40,43],"em",{},"\"Pff, easy task. Just run an ",[26,41,42],{},"ALTER TABLE"," and done. Why is this even a ticket?\"",[19,45,46,47,50],{},"But then I read the description.\nTurns out, ",[22,48,49],{},"FLOAT was causing data loss",", and I needed to convert it without losing data.",[19,52,53,54,57],{},"That’s when I realized: this isn’t just about one query. It’s about how ",[22,55,56],{},"FLOAT silently eats your money"," in MySQL.",[11,59,61],{"id":60},"why-float-is-a-problem","Why FLOAT is a Problem",[19,63,64,66,67,70],{},[26,65,28],{}," in MySQL is a ",[22,68,69],{},"binary floating-point type",".\nIt doesn’t store exact values — only approximations.",[19,72,73,74,77],{},"That’s fine for rocket science 🚀 or graphics rendering 🎮, but for ",[22,75,76],{},"money"," where every cent matters? Disaster.",[19,79,80,81,83,84,87,88,91],{},"Think of ",[26,82,28],{}," as a leaky bucket. You pour in ",[26,85,86],{},"$1,000,000.25","… and it gives you back ",[26,89,90],{},"$999,999.94",".",[19,93,94],{},"Not funny when it’s your salary.",[11,96,98],{"id":97},"example-of-data-loss","Example of Data Loss",[100,101,106],"pre",{"className":102,"code":103,"language":104,"meta":105,"style":105},"language-sql shiki shiki-themes dracula","CREATE TABLE money_float (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    amount FLOAT\n);\n\nINSERT INTO money_float (amount) VALUES (1000000.25), (123456789.99);\n\nSELECT * FROM money_float;\n","sql","",[26,107,108,116,122,128,134,141,147,152],{"__ignoreMap":105},[109,110,113],"span",{"class":111,"line":112},"line",1,[109,114,115],{},"CREATE TABLE money_float (\n",[109,117,119],{"class":111,"line":118},2,[109,120,121],{},"    id INT AUTO_INCREMENT PRIMARY KEY,\n",[109,123,125],{"class":111,"line":124},3,[109,126,127],{},"    amount FLOAT\n",[109,129,131],{"class":111,"line":130},4,[109,132,133],{},");\n",[109,135,137],{"class":111,"line":136},5,[109,138,140],{"emptyLinePlaceholder":139},true,"\n",[109,142,144],{"class":111,"line":143},6,[109,145,146],{},"INSERT INTO money_float (amount) VALUES (1000000.25), (123456789.99);\n",[109,148,150],{"class":111,"line":149},7,[109,151,140],{"emptyLinePlaceholder":139},[109,153,155],{"class":111,"line":154},8,[109,156,157],{},"SELECT * FROM money_float;\n",[19,159,160],{},"Result:",[162,163,164,177],"table",{},[165,166,167],"thead",{},[168,169,170,174],"tr",{},[171,172,173],"th",{},"id",[171,175,176],{},"amount",[178,179,180,189],"tbody",{},[168,181,182,186],{},[183,184,185],"td",{},"1",[183,187,188],{},"1000000.25",[168,190,191,194],{},[183,192,193],{},"2",[183,195,196],{},"123456792.00",[19,198,199,200,203,204,206],{},"We inserted ",[26,201,202],{},"123456789.99",", but got back ",[26,205,196],{},".\nThe bigger the number, the worse the corruption.",[11,208,210],{"id":209},"but-why-does-float-lose-data","But Why Does FLOAT Lose Data?",[19,212,213],{},"Here’s the fun part. Let’s make it simple.",[215,216,217,226],"ul",{},[218,219,220,222,223,91],"li",{},[26,221,28],{}," stores numbers in ",[22,224,225],{},"binary (base 2)",[218,227,228],{},"But not every decimal number can be written exactly in binary.",[19,230,231],{},"Example:",[215,233,234,241,254],{},[218,235,236,237,240],{},"In decimal, ",[26,238,239],{},"0.1"," is simple.",[218,242,243,244,246,247,250,251],{},"In binary, ",[26,245,239],{}," is ",[22,248,249],{},"infinite repeating",": ",[26,252,253],{},"0.0001100110011…",[218,255,256,257,259],{},"So ",[26,258,28],{}," cuts it off at some point and stores an approximation.",[19,261,262],{},"That’s why when you do:",[100,264,266],{"className":102,"code":265,"language":104,"meta":105,"style":105},"INSERT INTO money_float (amount) VALUES (0.1);\nSELECT amount FROM money_float;\n",[26,267,268,273],{"__ignoreMap":105},[109,269,270],{"class":111,"line":112},[109,271,272],{},"INSERT INTO money_float (amount) VALUES (0.1);\n",[109,274,275],{"class":111,"line":118},[109,276,277],{},"SELECT amount FROM money_float;\n",[19,279,280,281,91],{},"You might see something like ",[26,282,283],{},"0.10000000149",[19,285,286,287,290],{},"Now imagine this tiny error repeated in ",[22,288,289],{},"millions of dollars",".\nErrors pile up, and suddenly your 9-digit amount looks… off.",[11,292,294],{"id":293},"decimal-to-the-rescue","DECIMAL to the Rescue",[19,296,297,300],{},[26,298,299],{},"DECIMAL"," stores numbers differently:",[215,302,303,310],{},[218,304,305,306,309],{},"Instead of binary approximation, it stores ",[22,307,308],{},"exact digits as strings"," internally.",[218,311,312,313,315,316,91],{},"That means ",[26,314,202],{}," is stored as exactly ",[26,317,202],{},[100,319,321],{"className":102,"code":320,"language":104,"meta":105,"style":105},"CREATE TABLE money_decimal (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    amount DECIMAL(15,2)\n);\n\nINSERT INTO money_decimal (amount) VALUES (1000000.25), (123456789.99);\n\nSELECT * FROM money_decimal;\n",[26,322,323,328,332,337,341,345,350,354],{"__ignoreMap":105},[109,324,325],{"class":111,"line":112},[109,326,327],{},"CREATE TABLE money_decimal (\n",[109,329,330],{"class":111,"line":118},[109,331,121],{},[109,333,334],{"class":111,"line":124},[109,335,336],{},"    amount DECIMAL(15,2)\n",[109,338,339],{"class":111,"line":130},[109,340,133],{},[109,342,343],{"class":111,"line":136},[109,344,140],{"emptyLinePlaceholder":139},[109,346,347],{"class":111,"line":143},[109,348,349],{},"INSERT INTO money_decimal (amount) VALUES (1000000.25), (123456789.99);\n",[109,351,352],{"class":111,"line":149},[109,353,140],{"emptyLinePlaceholder":139},[109,355,356],{"class":111,"line":154},[109,357,358],{},"SELECT * FROM money_decimal;\n",[19,360,160],{},[162,362,363,371],{},[165,364,365],{},[168,366,367,369],{},[171,368,173],{},[171,370,176],{},[178,372,373,379],{},[168,374,375,377],{},[183,376,185],{},[183,378,188],{},[168,380,381,383],{},[183,382,193],{},[183,384,202],{},[19,386,387],{},"Perfect. ✅ No rounding surprises.",[11,389,391],{"id":390},"why-alter-wont-save-you","Why ALTER Won’t Save You",[19,393,394],{},"Here’s the trap I fell into:",[100,396,398],{"className":102,"code":397,"language":104,"meta":105,"style":105},"ALTER TABLE money_float MODIFY amount DECIMAL(15,2);\n",[26,399,400],{"__ignoreMap":105},[109,401,402],{"class":111,"line":112},[109,403,397],{},[19,405,406],{},"You’d think this fixes it, right?\nNope. ❌",[19,408,409,410,412,413,416,417,91],{},"The data was already corrupted when it was first inserted as ",[26,411,28],{},".\n",[26,414,415],{},"ALTER"," just moves the already-broken value into ",[26,418,299],{},[19,420,421],{},"Garbage in → garbage out.",[11,423,425],{"id":424},"visual-float-vs-decimal","Visual: FLOAT vs DECIMAL",[100,427,432],{"className":428,"code":430,"language":431},[429],"language-text","FLOAT (approximation in binary):\n123456789.99  --->  123456792.00 💀\n\nDECIMAL (exact digits):\n123456789.99  --->  123456789.99 ✅\n","text",[26,433,430],{"__ignoreMap":105},[11,435,437],{"id":436},"lessons-learned","Lessons Learned",[215,439,440,450,461],{},[218,441,442,443,445,446,449],{},"Never use ",[26,444,28],{},"/",[26,447,448],{},"DOUBLE"," for money.",[218,451,452,453,456,457,460],{},"Always use ",[26,454,455],{},"DECIMAL(precision, scale)"," (e.g., ",[26,458,459],{},"DECIMAL(15,2)",").",[218,462,463,464,466,467,469],{},"If your table already has money in ",[26,465,28],{},", you cannot fix the lost precision with ",[26,468,415],{},". You’ll need to re-import or clean it at the source.",[11,471,473],{"id":472},"final-thought","Final Thought",[19,475,476,477,479,480,483],{},"Using ",[26,478,28],{}," for money is like paying your salary in ",[22,481,482],{},"Monopoly money",". 🎲💵\nIt looks okay until you try to spend it — then you realize it’s worthless.",[19,485,486,487,489],{},"Stick with ",[26,488,299],{},", and your dollars will stay safe. ✅",[491,492,493],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":105,"searchDepth":118,"depth":118,"links":495},[496,497,498,499,500,501,502,503,504],{"id":13,"depth":124,"text":14},{"id":60,"depth":124,"text":61},{"id":97,"depth":124,"text":98},{"id":209,"depth":124,"text":210},{"id":293,"depth":124,"text":294},{"id":390,"depth":124,"text":391},{"id":424,"depth":124,"text":425},{"id":436,"depth":124,"text":437},{"id":472,"depth":124,"text":473},"Recently I got a task Alter a table column from `FLOAT` to `DECIMAL(10,2)","md",{"date":508,"image":509,"alt":6,"tags":510,"published":139},"19th Sep 2025","/blogs-img/blog8.png",[511,512,513],"mysql","float","decimal","/blogs/float-make-my-dollar-float-away",{"title":6,"description":505},"blogs/8. float-make-my-dollar-float-away","LLMtdDXEF2PNMbVJsdAFzOmMKY39dzRGJa9XONZHDaI",1770700754811]