How can I convert my string to a number in Javascript? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert String variable to int in javascript?
I have the following:
var dataElapsed = $("#stats-list").attr("data-elapsed");
This creates a string called dataElapsed but I expect a number. How can I convert this?
Is there some way to convert with JQuery? or do I have to use some Javascript way?

Try this(assuming the data attributes have valid numeric values):
var dataElapsed = parseFloat($("#stats-list").attr("data-elapsed")); //for decimal.
or
var dataElapsed = parseInt($("#stats-list").attr("data-elapsed"), 10); //for integer.

You can use the .data() method to get values from data attributes
var dataElapsed = $("#stats-list").data("elapsed");

Related

How to split a string using comma? [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 2 years ago.
I have a string like this
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
how do i print only first three strings like this "NAME1,NAME2,NAME3" in JavaScript
You can do this way
let string= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6".split(',')
let output = string.splice(',',3).join(',')
console.log(output)
Using standard Array functions you can do what you want. The code below shows the various stages, though you can write it more concisely as one line if you like.
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
var splitAssignements = assignments.split(",")
var firstThreeArray = splitAssignements.slice(0,3)
var firstThreeString = firstThreeArray.join(",")
console.log(firstThreeString)

Getting wrong parsing with JSON.parse? [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)
Closed 2 years ago.
I have used the following text as input to JSON.parse()
let inputTxt = `{"result":[{"aliasName":null,"name":"SRDD","sort":0,"id":319488404634063872,"parentId":319472086895578112,"level":2},{"aliasName":null,"name":"Noodles","sort":11,"id":350368638463726592,"parentId":350368638434366464,"level":2},{"aliasName":null,"name":"Sushi","sort":21,"id":350368638463726593,"parentId":350368638434366464,"level":2},{"aliasName":null,"name":"Drink","sort":31,"id":350368638463726594,"parentId":350368638434366464,"level":2},{"aliasName":null,"name":"Set","sort":41,"id":350368739890458624,"parentId":350368638434366464,"level":2},{"aliasName":"默认","name":"默认","sort":1,"id":319472086895578112,"parentId":null,"level":1},{"aliasName":null,"name":"Tendongo","sort":0,"id":350368638434366464,"parentId":null,"level":1}],"code":0,"message":"success[OK]","messageUuid":"437fcce5209d4d17b558403d4a8b859c","apiMessage":null}`
console.log(JSON.parse(inputTxt))
Here you can see the id for the category is different in the output.
The issue is with a big number. When you parse JSON. It is trying to hold a large number.
Max value can be seen as :
Number.MAX_SAFE_INTEGER // (253 - 1) => 9007199254740991
More: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
const str = "319488404634063872"
console.log(Number(str)) // 319488404634063900
// OR
const num = 319488404634063872
console.log(num) // 319488404634063900
You can use replace function as workaround, before parsing convert it to string.
let inputTxt = `{"result":[{"aliasName":null,"name":"SRDD","sort":0,"id":319488404634063872,"parentId":319472086895578112,"level":2},{"aliasName":null,"name":"Noodles","sort":11,"id":350368638463726592,"parentId":350368638434366464,"level":2},{"aliasName":null,"name":"Sushi","sort":21,"id":350368638463726593,"parentId":350368638434366464,"level":2},{"aliasName":null,"name":"Drink","sort":31,"id":350368638463726594,"parentId":350368638434366464,"level":2},{"aliasName":null,"name":"Set","sort":41,"id":350368739890458624,"parentId":350368638434366464,"level":2},{"aliasName":"默认","name":"默认","sort":1,"id":319472086895578112,"parentId":null,"level":1},{"aliasName":null,"name":"Tendongo","sort":0,"id":350368638434366464,"parentId":null,"level":1}],"code":0,"message":"success[OK]","messageUuid":"437fcce5209d4d17b558403d4a8b859c","apiMessage":null}`;
const format = (json) => json.replace(/\"id\":(\d+)/g, `"id":"$1"`)
console.log(JSON.parse(format(inputTxt)))

How do I convert my HEX string into a number? [duplicate]

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

Convert each value in the array to string [duplicate]

This question already has answers here:
Convert integer array to string array in JavaScript
(11 answers)
Closed 6 years ago.
I have this array
var p = [0,1,2,3,4];
and I want to convert each value in the array to string like
var p = ['0','1','2','3','4'];
any ideas, help please?
You can convert number to string simply by prepending empty string to it.
p = p.map(function(e){return ""+e});
All you need to do is pass the Number value to the String function to get a string out of a Number.
[0,1,2,3,4].map(function(value) { return String(value); });

Javascript/jquery get number value inside of element [duplicate]

This question already has answers here:
get element value using jquery
(2 answers)
Closed 9 years ago.
<span id="rate">2388</span>
var = $("#rate")...
I need get number value inside of element. any one know how to do it?
var number = +$("#rate").text();
// | \______get text of span as string
// |_________________unary plus converts to numeric
You can get the text inside your span using text() method and parseInt() to convert it to number:
var rateVal = parseInt($("#rate").text(), 10);
You can do also this
var result = Number($("#rate").html());
But becareful to use Number functon, Please have look this discussion for difference between parseInt and Number.
What is the difference between parseInt(string) and Number(string) in JavaScript?, as Roko indicated.

Categories

Resources