0

I need to store values in PostgreSQL database where 12 numbers and 6 decimal. According to PostgreSQL it provides numeric datatype. I created a field with numeric(18,6), but when I am entering value, it is just accepting 15 digits values total.

For e.g. If i try to store number 123456789123.123456, it truncates number and stores only 123456789123.123.

In JavaScript, when I am trying to parse a string of 18 digits including 6 decimals it returns only 17 digits, 12 numbers and 5 decimals.

For e.g.

var number = "123456789123.123456"
console.log(parseFloat(number));

it prints only 123456789123.12346

Is there any solution about this?

1 Answer 1

2

This is a limitation of the double-precision floating point data type and is not unique to JavaScript or Postgres. Postgres can use the same double-precision floating point type, or, as you have done, support an arbitrary precision number which is stored in a different format.

JavaScript only has one numeric data type: double-precision floating point.

Keep in mind that this is a precision limitation, not "17 digits". You can successfully store 0.0000000000000003939393928293 or 8949493210129000000000000000000000000000.

If you need to work with numbers of greater precision, you can use a library like bignumber.js. You lose the ability to use the simple operators, so num3 = num1 + num2 becomes num3 = num1.plus(num2), but it should do what you need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.