Javascript long integer
In Java, you have 64 bits integers, and that's what you're using.
In JavaScript, all numbers are 64 bits floating point numbers. This means you can't represent in JavaScript all the Java longs. The size of the mantissa is about 53 bits, which means that your number, 793548328091516928
, can't be exactly represented as a JavaScript number.
If you really need to deal with such numbers, you have to represent them in another way. This could be a string, or a specific representation like a digit array. Some "big numbers" libraries are available in JavaScript.
How can I convert long to int in JavaScript?
Use the parseInt()
function like so:
var d = new Date();
function myFunction() {
/*console.log("onload()");
console.log(d);*/
document.getElementById("myBtn").innerHTML = d.toDateString();
}
function calcola() {
var input = document.getElementById("dataCorrente").value;
if (input != null) {
var dat = new Date();
dat.setHours(0);
dat.setMinutes(0);
dat.setSeconds(0);
var data = new Date(input);
var mill1 = dat.getTime();
var mill2 = data.getTime();
if (mill2 > mill1) {
mill2 = mill2 - mill1;
var ris = mill2 / 86400000;
document.getElementById("Risultato").innerHTML = parseInt(ris);
} else {
document.getElementById("Risultato").innerHTML = "La data deve essere maggiore di quella corrente";
}
} else {
console.log("input=null");
document.getElementById("Risultato").innerHTML = "Data non inserita";
}
}
<!DOCTYPE html>
<html>
<head>
<title>Calcolo distanza</title>
</head>
<body onload="myFunction()">
<p> Data corrente </p>
<button type="button" id="myBtn"></button>
<p> Scegli la data </p>
<input type="date" id="dataCorrente">
<input type="submit" onclick="calcola()">
<br>
<p id="Risultato"> </p>
</body>
</html>
What makes long integer numerical values (16+ chars) to mutate in JavaScript?
In JavaScript, all numbers are 64 bits floating point numbers.
The size of the mantissa is about 53 bits, which means that your number, 154688977320418257
, can't be exactly represented as a JavaScript number. What you see is an approximation, because it's a number higher than MAX_SAFE_INTEGER
(i.e 9007199254740991).
If you really need big numbers, you could use a lib such as https://github.com/peterolson/BigInteger.js.
Conversion issue for a long string of integers in JavaScript
BigInt
is now available in browsers.
BigInt
is a built-in object that provides a way to represent whole
numbers larger than 253, which is the largest number JavaScript can
reliably represent with the Number primitive.value The numeric value of the object being created. May be a string or an integer.
var strOne = '123456789123456789122';
var intOne = BigInt(strOne);
var strTwo = '1234567891234567891232';
var intTwo = BigInt(strTwo);
console.log(intOne, intTwo);
Javascript long integer from server is not accurate
See my solution on this question:
Transform the response to string, then apply a repalce with a regex to
convert Id field to string type:const axios = require("axios");
axios.get(url, {transformResponse: [data => data]}).then((response) => {
let parsed = JSON.parse(response.data.replace(/"Id":(\d+),/g, '"Id":"$1",'))
console.log(parsed)
});
How Do I Set a Long Integer Property in Gremlin JavaScript?
Use toLong()
in exports.toLong
in gremlin-javascript/src/main/javascript/gremlin-javascript/lib/utils.js
public static async experiment(): Promise<any> {
const g = GremlinDb.g;
await g.addV("test")
.property("number", toLong(3)).iterate();
return {};
}
What is JavaScript's highest integer value that a number can go to without losing precision?
JavaScript has two number types: Number
and BigInt
.
The most frequently-used number type, Number
, is a 64-bit floating point IEEE 754 number.
The largest exact integral value of this type is Number.MAX_SAFE_INTEGER
, which is:
- 253-1, or
- +/- 9,007,199,254,740,991, or
- nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one
To put this in perspective: one quadrillion bytes is a petabyte (or one thousand terabytes).
"Safe" in this context refers to the ability to represent integers exactly and to correctly compare them.
From the spec:
Note that all the positive and negative integers whose magnitude is no
greater than 253 are representable in theNumber
type (indeed, the
integer 0 has two representations, +0 and -0).
To safely use integers larger than this, you need to use BigInt
, which has no upper bound.
Note that the bitwise operators and shift operators operate on 32-bit integers, so in that case, the max safe integer is 231-1, or 2,147,483,647.
const log = console.logvar x = 9007199254740992var y = -xlog(x == x + 1) // true !log(y == y - 1) // also true !
// Arithmetic operators work, but bitwise/shifts only operate on int32:log(x / 2) // 4503599627370496log(x >> 1) // 0log(x | 1) // 1
How to convert a long string (more than 16 digits) into numbers
Just expanding on my above comment with another solution...
You've exceeded the maximum safe integer value. (Number.MAX_SAFE_INTEGER
, which equals 9007199254740991
). Numbers larger than this are not supported with standard integer types in javascript, or rather there's not enough precision to represent them. Anything larger than this is represented in scientific notation and the extra digits are truncated and represented only as zeroes.
With that said, you don't even need to convert the array to a string to an integer just to increment it. You can just increment the individual digits in the array, starting at the end and working your way forwards to "carry the 1" so to speak.
var plusOne = function(digits) {
for(let i = digits.length - 1; i > -1; i--)
{
if(digits[i] == 9)
{
digits[i] = 0;
if(i == 0)
digits = [1].concat(digits);
}
else
{
digits[i]++;
break;
}
}
return digits;
};
console.log(plusOne([6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]));
Extremely large numbers in javascript
You are going to need a javascript based BigInteger library. There are many to choose from. Here is one https://github.com/peterolson/BigInteger.js
You can use it like this
var n = bigInt("91942213363574161572522430563301811072406154908250")
.plus("91942213363574161572522430563301811072406154908250");
Related Topics
Version Control for One-Man Project Using Eclipse
Language Detection with Data in Postgresql
How to Script the Java Debugger Command-Line Tool (Jdb)
What Does a Java Static Method Look Like in Ruby
What Would Be the Fastest Method to Test for Primality in Java
Why Is the Range of Bytes -128 to 127 in Java
How to Start Two Threads at "Exactly" the Same Time
Why Ruby Modulo Is Different from Java/Other Lang
How to Get from Jruby a Correctly Typed Ruby Implementation of a Java Interface
Fixing 'Path' in Environment Variables in Windows 7 for Multiple Applications
Python: How to Execute a Jar File Through a Python Script
Why Is Double.Min_Value in Not Negative
Is Iterating Concurrenthashmap Values Thread Safe
Create Random Pixel Images in Swift
How to Do a Cross Platform Encryption Method Working in Both iOS and Android (Aes Encryption Only..)
Why Should Wait() Always Be Called Inside a Loop