Skip to content

parse numeric types as string #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ The two share the same interface so __no other code changes should be required__
* async notifications with `LISTEN/NOTIFY`
* bulk import & export with `COPY TO/COPY FROM`
* extensible js<->postgresql data-type coercion

### numeric datatype
Since the `numeric` datatype may have an arbitrary precision,
values of this datatype will be returned as a string from a query. If you need
to do any computations on the value, you should be aware of this, as you may
need to use a JavaScript library for handling the value of arbitrary precision.

Simply taking the value and doing any computation such as addition or subtraction
may lead to unexpected/wrong results since JavaScript will handle the value as
a floating-point number.

## Documentation

Expand Down
10 changes: 0 additions & 10 deletions lib/textParsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ var parseByteA = function(val) {
}
};

var maxLen = Number.MAX_VALUE.toString().length;

var parseInteger = function(val) {
return parseInt(val, 10);
};
Expand All @@ -167,14 +165,6 @@ var init = function(register) {
register(21, parseInteger);
register(23, parseInteger);
register(26, parseInteger);
register(1700, function(val){
if(val.length > maxLen) {
console.warn(
'WARNING: value %s is longer than max supported numeric value in ' +
'javascript. Possible data loss', val);
}
return parseFloat(val);
});
register(700, parseFloat);
register(701, parseFloat);
register(16, parseBool);
Expand Down
2 changes: 1 addition & 1 deletion test/integration/client/type-coercion-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var types = [{
},{
//TODO get some actual huge numbers here
name: 'numeric',
values: [-12.34, 0, 12.34, null]
values: ['-12.34', '0', '12.34', null]
},{
name: 'real',
values: [101.1, 0, -101.3, null]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/client/typed-query-results-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('typed results', function() {
format: 'text',
dataTypeID: 1700,
actual: '12.34',
expected: 12.34
expected: '12.34'
},{
name: 'real/float4',
dataTypeID: 700,
Expand Down