question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numeric data type is returned as String

See original GitHub issue

Hi, firstly thanks for this module, it’s really great. I have found a problem in numeric data type. It’s parsed as String.

Maybe I did something wrong or I have used bad DB type. Thanks for any advice.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

37reactions
illzcommented, May 1, 2019

Numeric type is oid 1700, so you can add this in your app.js or similar:

var types = require('pg').types
types.setTypeParser(1700, function(val) {
    return parseFloat(val);
});
20reactions
bendruckercommented, Jun 29, 2015

This actually does depend on your column type (http://www.postgresql.org/docs/9.4/static/datatype-numeric.html)

The type parsing here is handled by https://github.com/brianc/node-pg-types

If you dig into the readme, you’ll see an example about int8 (64 bit). Those can’t be parsed using parseInt because JS doesn’t support 64 bit ints.

If you use a specific int type (smallint / 2 bytes, integer / 4 bytes) you’ll get the Number you’re expecting. But because numeric can be way larger than Number.MAX_SAFE_INTEGER, the only safe way to parse it is to return a string with full precision.

The good news is that if you know your numerics are within JS’s range, you can override the parser as instructed over at pg-types. Or, even better, you can use a more restrictive column type and it’ll happen automatically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

knex postgres returns strings for numeric/decimal values
Any way to override this behavior? If you are going to convert decimal number strings -> javascript numbers you must be extra careful...
Read more >
Converting from Numeric to String :: Data Types (Programming)
The char function converts integers to Unicode character codes and returns a string composed of the equivalent characters: x = [77 65 84...
Read more >
Type Conversion Functions - Microsoft Support
Returns "ProductID",converts the values in "Discount" field into Double for numeric values and String for Non-numeric values. VBA examples. Note: Examples that ...
Read more >
API - Datatypes | Tedious
Type Constant1 JavaScript Result set Parameter Exact numerics Exact numerics Exact numerics Exact numerics bit TYPES.Bit Boolean ✓ ✓ tinyint TYPES.TinyInt Number ✓ ✓
Read more >
The Type Hierarchy - SQLAlchemy 1.4 Documentation
The “CamelCase” datatypes¶. The rudimental types have “CamelCase” names such as String , Numeric , Integer , ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found