javascript JSON.parse wrong timestamp [duplicate] - javascript

This question already has an answer here:
Variable increases by 1 on high numbers when I don't want to
(1 answer)
Closed 8 years ago.
"JSON.parse" parsing timestamp not correctly.
strObj='{"Timestamp":635450757182431418}';
console.log ('String object:' + strObj ); // Timestamp":635450757182431418
var parseObj= JSON.parse (strObj);
console.log (parseObj); // Timestamp: 635450757182431400
http://jsfiddle.net/kwakwak/rqb6gf4z/
before parse: 635450757182431418,
after parse: 635450757182431400
What is the problem?
Thanks!

635450757182431418 is too big for the JavaScript format number, which is IEEE754 double precision, meaning there's about 53 bits for the integer part.
This number can't be exactly represented as a JavaScript number, you should use a different format (string, digit array, custom).
If you want to get the timestamp as a string, you might do this :
var strTimestamp = strObj.match(/"Timestamp"\s*:\s*(\d+)/)[1];

When you have a string 635450757182431418 value is stored, but as a number it's rounded to 635450757182431400.
Just type this in chrome console
635450757182431418
You will get result as 635450757182431400 because Number doesn't support greater than 635450757182431400 in JS.

Related

Javascript convert string value to Int value or Float [duplicate]

This question already has answers here:
How can I parse a string with a comma thousand separator to a number?
(17 answers)
Closed last month.
I want to convert string to Int or float type in JavaScript below is my code. or any solution is there in react library?
var a = '23,34.0';
console.log(parseFloat(a)); // 23
console.log(parseInt(a)) // 23
I want output like this:
`var a = '23,34.0';
output :
23,34.0 as a integer
I tried below codes
parseInt(a) parseFloat(a) Number(a)
this methods I tried but am not exact outputs.
Since your string is not a valid number, you cannot convert it.
You have to pass a valid number like 2324.5 to convert.
Trying this
var a = '2334.5'
console.log(parseFloat(a))
console.log(parseInt(a))
You will get 2334.5 and 2334 as output. "parseInt" will still yet cut of everything after the decimal point, because that's what an integer is, a whole number.

Convert a string to number with parseInt does not work as expected [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
How to convert a String to long in javascript?
(2 answers)
Closed 10 months ago.
A quick maybe simple question :
We are trying to parse a string into number , the sample number is "1928433000460244141" so when we try to parse it to integer we get 1928433000460244200
const no1 = "1928433000460244141" ;
console.log(parseInt(no1)); // returns 1928433000460244200
what can cause this problem and what is the solution ?
BigInt can be used to store the data , but the problem is we want to send the string converted to number to a service we are using right now , we do not have any access hence it is a 3rd party service so we should handle it from our side.
The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1.
Source
Because your number is larger than the safe integer limit, you're likely experiencing some floating-point precision issues that are rounding the value off.

String to int rounding number on nodeJS app [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Closed 1 year ago.
I have a string as numbers. And I want to transform string to int.
So my code like that:
const bigNumber = '6972173290701864962'
console.log(bigNumber)
//6972173290701864962 =====> last digits : *****1864962
console.log(Number(bigNumber))
//6972173290701865000 =====> last digits : *****1865000
Why Im getting rounding number? How can I solve this problem?
The number is greater than Number.MAX_SAFE_INTEGER.
Instead, cast a to a BigInt:
const bigNumber = '6972173290701864962'
console.log('Number: '+Number(bigNumber))
console.log('BigInt: '+BigInt(bigNumber))
To remove the trailing n, simply call toString():
const bigNumber = '6972173290701864962'
console.log(BigInt(bigNumber).toString());

How to convert string to Long (not long) in JavaScript/TypeScript [duplicate]

This question already has answers here:
How to convert a String to long in javascript?
(2 answers)
Closed 3 years ago.
The backend API implementation is expecting an array of Long .
I need to pass the value through front-end which is a GraphQL implementation. Initially I have the value as a string and want to convert it to an array of Long so that it is compatible with the API.
I have tried the following:
input1 = [JSON.parse(input1)]
The desired payload should be as follows where 140 is a "Long"
"rolls": [140]
I currently see my graphQL query variables as follows where 140 is a string.
rolls: 140
Currently, I am seeing the GraphQL error:
use Number to convert value from string to number
const someValueStr = "140";
const someValueNum = Number(someValue);
in JavaScript there is number type not long or double or int
Typescript (the whole javascript actually) has just one data type for numbers, i.e Number, and they store numbers in 64 bits (or double-precision floating point format).
There is a library called BigInteger.js which can help you to represent numbers bigger than what javascript supports.

Why does parsing a large number, of string form, give a larger number? [duplicate]

This question already has answers here:
Why do JavaScript converts some number values automatically
(5 answers)
Closed 7 years ago.
See snippet:
var num = "9223372036854775808";
document.write(num);
document.write("<br>");
document.write(parseInt(num, 10));
On running the code snippet, the first write yields:
9223372036854775808
and the third write yields:
9223372036854776000
But, I am only parsing the number in string form to number. Why does it give a still larger number?
I thought it might have to do something with limits of storage capacity, but then, why would it yield a larger number if it could not store the smaller?
I read this question: Why parsing a very large number to integer return 1 and the parseInt doc but they did not help much.
So, Why does parsing a large number, of string form, return a larger number?
The first result is fine as it is treated as a string.
In the second one, it crosses the value of int which is +/- 9007199254740992 ie, the maximum value which parseint can parse is 9007199254740992 and since your value 9223372036854775808 is larger than the maximum value. it is giving some garbage value.
As commented correctly by blunderboy it seems that that the reason could be this
The numbers in Javascript are 64 bit "double" precision which follow the IEE754 floating point. Sothe largest positive whole number that can therefore be accurately represented is 2^53 and the rest of the remaining bits are reserved for the exponent.
If you check the ECMA specs this has been explained.

Categories

Resources