Skip to content

parseFloat unsuitable for NUMERIC type #107

Closed
@chrisfarms

Description

@chrisfarms

I think columns of type NUMERIC should be returned as Strings rather than native javascript numbers.

The NUMERIC type is for arbitrary precision numbers. Numbers which will fall way outside of what the native max size from V8 will be. Currently if I have huge integers in my database, then fetch them out with node-postgres, it gets run through parseFloat, truncated and I'm left with a different number:

var pg = require('pg');

var crazyBigInt = '294733346389144765940638005275322203805';

pg.connect("tcp://localhost/test", function(err,client){ 
    if(err) throw err; 
    // make an example table with numeric col of arbitary precision
    client.query("CREATE TABLE example (id numeric)");
    client.query("INSERT INTO example (id) VALUES ($1)", [crazyBigInt]);
    client.query("SELECT * FROM example", function(err,res){
        var n = res.rows[0].id;
        // check string equality
        console.log("Expected result from numeric column to be", crazyBigInt, "but got", n.toString());
        // clean up
        client.query("DROP TABLE example", function(err,res){
            process.exit();
        });
    });
});

I think including some kind of BigInt implementation within node-postrges would just cause more problems, so I think it would be best if it just always returned strings for numeric columns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions