pg-promise returns decimal as string values

984 Views Asked by At

pg-promise returns the decimal values as string. How do I fix this?

select round(avg(numcol),2)::decimal from details
1

There are 1 best solutions below

0
On

Following the links I provided in the comments (one and two), you should see that JavaScript simply doesn't have the required precision, so automatic conversion may result in lost precision.

If you do not care about that, you can apply JavaScript standard parseFloat:

const types = pgp.pg.types; // types conversion interface

// for real/float4:
types.setTypeParser(700, parseFloat);

// for double precision/float8:
types.setTypeParser(701, parseFloat);

// for arrays of float4:
const parseFloat4Array = types.getTypeParser(1021);
types.setTypeParser(1021, a => parseFloat4Array(a).map(parseFloat));

// for arrays of float8:
const parseFloat8Array = types.getTypeParser(1022);
types.setTypeParser(1022, a => parseFloat8Array(a).map(parseFloat));

Otherwise, use a specialized library for dealing with decimals.