1

I am using BigInt(20) datatype for auto Increment id in mysql database. and when the integer value is so big then how can I handle this as after the number precision of javascript, it won't to allow to insert and read any number Value. So how can I achieve this. Read about the big-integer libraries but I won't the expected result

Example:-

var x = 999999999999999999999999999999999999999;

How can I print the same number without using its exponential value and any garbage value ?

I tried like that

var BigNumber = require('big-number');

var x = new BigNumber(999999999999999999999999999999999999999, 10);
console.log(x);

Example2:-

If I get the last inserted Id, then how can I handle this value

 connection_db.query('INSERT INTO tableName SET ?', tableData, 
  function (error1, results1, fields1) {
     error1){
         // Db error
        }else{
         var lastInserted = new BigNumber(results1.insertId);
         console.log(lastInserted);// still wrong value
          }

  });
2
  • 1
    Don't think BIGINT(20) generates a int with 20 digits.. the (20) only apply to zerofull function of mysql... the minimal and maximal values off BIGINT are here dev.mysql.com/doc/refman/5.5/en/integer-types.html.. Commented Sep 26, 2017 at 13:55
  • 2
    @robertklep provides a good answer. I want to add another thought. Depending on your concrete environment, JavaScript gives you "safe integers" up to 2^53 - 1. MySQL's BIGINT is (depending on your installation) a 64 bit value. So, if 2^53 - 1 (= 9007199254740991) is enough for your needs, you might use this combination anyway (taking care of your bounds, of course). Commented Sep 26, 2017 at 14:10

1 Answer 1

3

You can only pass/show large numbers like that as strings:

var BigNumber = require('big-number');

var x = new BigNumber('999999999999999999999999999999999999999', 10);
console.log(x.toString())

However, in the end, it's up to the MySQL driver how it handles large numbers like this, because it does have to take into account Number.MAX_SAFE_INTEGER.

For instance, the mysql module has various options (supportBigNumbers and bigNumberStrings) that relate to handling BIGINT.

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

5 Comments

It's worth clarifying: If your application requires integer values with more than 14 or 15 digits, you're in for a world of pain if you try to program the application in Javascript. If you can, avoid it.
@robertklep How can I even perform like this becoze some is sending the data as very big number so how can I pass this as a sttring
@VIKASKOHLI see my edit. You probably have to specifically enable BIGINT support in the MySQL driver.
@robertklep So I have to change the sql package for supportig the very big numbers
@VIKASKOHLI no, you have to set the correct option. It already supports big numbers, but that support is disabled by default. Please read the documentation.

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.