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.
Related
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());
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.
This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 3 years ago.
I wish to convert the whiteHex variable to a decimal using the parseInt() function, and store it in a variable, whiteDecimal.
var whiteHex = 'ffffff';
var whiteDecimal = parseInt(whiteHex);
I am unsure if the above is correct or not. The reason being, that I then wish to subtract 1 from whiteDecimal and store it in a variable offWhiteDecimal. This is where I am getting stuck. How can I subtract one from the ffffff hex value? Am I missing something within the parseInt function?
You're looking for this:
var whiteDecimal = parseInt(whiteHex, 16)
console.log(whiteDecimal - 1);
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Syntax
This question already has answers here:
Decoding hex-containing escape sequences in JavaScript strings
(6 answers)
Closed 2 years ago.
I've got this string containing multiple hexadecimal numbers:
let input = '\x01\x01\x02\x01';
I would like to see this transformed into: 1121 (the decimal representation of each hex number)
How would I go about this? I've tried numerous things, but the only output I get are some diamond shapes with a questionmark inside of it or syntax errors. Many thanks in advance!
Here is a simple method that escapes the string, removes empty elements, converts each number to is decimal digit, joins it as a string, and then converts the final result to a number.
function convert(string) {
string = escape(string);
string = string.split(/%/).filter(e=>e);
string = string.map(e => +("0x"+e));
return +string.join("");
}
// Test case
var decimal = convert('\x01\x01\x02\x01');
console.log(decimal);
To be able to work with the escape sequences itself and not the resulting whitespace, you habe to use String
raw then you can replace:
let input = String.raw`\x01\x01\x02\x01`;
console.log(
input
.replace(/\\x01/g, "1")
.replace(/\\x02/g, "2")
);
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.