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
}
});
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
}
});
Share
Improve this question
edited Sep 26, 2017 at 14:07
VIKAS KOHLI
asked Sep 26, 2017 at 13:51
VIKAS KOHLIVIKAS KOHLI
8,4804 gold badges51 silver badges66 bronze badges
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./doc/refman/5.5/en/integer-types.html.. – Raymond Nijland 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 bination anyway (taking care of your bounds, of course). – iquellis Commented Sep 26, 2017 at 14:10
1 Answer
Reset to default 3You 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
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745421448a4626981.html
评论列表(0条)