Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing money amounts in mysql

I want to store 3.50 into a mysql table. I have a float that I store it in, but it stores as 3.5, not 3.50. How can I get it to have the trailing zero?

like image 904
David Avatar asked Feb 12 '10 11:02

David


People also ask

Is there a money data type in MySQL?

We can store the money values in MySQL in decimal(value1,value2).

How do you store money in a database?

The first important rule is, always store values as fractional units. It can be tempting to store a value such as $10 as 10.00 in your database. However, storing monetary values with a decimal point can cause a lot of issues. Instead, you should always store monetary values in their minor unit form.

Which data type is best for storing currency value?

Correct Option: A. Currency is a numeric information. For monetary calculations, FLOAT and DOUBLE are subject to rounding error and may not be suitable. A DECIMAL(M, 2) type is best suited for it.


1 Answers

Do not store money values as float, use the DECIMAL or NUMERIC type:

Documentation for MySQL Numeric Types

EDIT & clarification:

Float values are vulnerable to rounding errors are they have limited precision so unless you do not care that you only get 9.99 instead of 10.00 you should use DECIMAL/NUMERIC as they are fixed point numbers which do not have such problems.

like image 135
Morfildur Avatar answered Sep 22 '22 14:09

Morfildur