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

Sqlite math operations with REAL column ignore decimal digits

I built a table with a REAL column called QTY but when I try to apply simple math operations it ignores the decimal digits.
Here an example:

SELECT QTY, QTY*2.0, QTY+1.0 FROM BOM
QTY QTY*2.0 QTY+1.0
0,67 0.0 1.0
1,00 2.0 2.0
0,50 0.0 1.0

The QTY type is REAL, here the create table command:

CREATE TABLE IF NOT EXISTS bom(
                    BOM_ID text NOT NULL,
                    RM_FK text NOT NULL,
                    QTY REAL NOT NULL,
                    PRIMARY KEY (BOM_ID, RM_FK))

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

>Solution :

Sqlite uses Flexible Typing.

Even if you declared QTY as REAL, sqlite will accept and store any value, even text, converting it to REAL if it can, or storing it as TEXT if it can’t.

As pointed out by @forpas, you inserted ‘0,67’ in QTY, which sqlite couldn’t convert in REAL because the decimal separation symbol is ‘.’ and not ‘,’.

So, when doing your computation, it just uses the numbers before the ‘,’ discarding what comes after.

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