Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can a MySQL field store an INT and a Decimal?

I have a field in a MySQL table called quantity.

I would like the quantity field to be able to store the following numbers: 0.25, 0.5, 0.75 and 1.

I currently have the field set as DECIMAL(2,2) as according to the MySQL docs.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It all works well, except for the 1, it always stores in the field as 0.99. Is there a way I can make this store as 1 instead of 0.99?

I have also tried passing in 1.0 and 1.00, but this also stores as 0.99.

>Solution :

DECIMAL(2,2) defines a two-digit number with two decimal places. It will only store two decimal digits. 0.99 is the closest it can get to 1.0

Use DECIMAL(3,2) – that’s a three-digit number with two decimal places. That will allow space for the integer portion.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading