convert a big number string to int - javascript

i have this html
<p class="res_current tooltip" name="2.969.226.853.669.634.301.755.392" id="current_metal">3 Q</p>
im using this code to get the number of name:
var a = document.getElementById('current_metal').name;
i need to use a to compare like
if(a < 10) { do something }
but the problem is that when i see a (using alert) it return something like this
-> 2,96922685366e+24
so i cant do the if :(, there is a way to get the FULL number without the "."? or convert it

Use parseInt:
parseInt(a, 10);
CAUTION: Do not omit the radix argument!

Related

Correctly format a whole number to a 2 decimal places output in JS but put 0 in front [duplicate]

I have a script which returns a price for a product. However, the price may or may not include trailing zeros, so sometimes I might have 258.22 and other times I might have 258.2. In the latter case, I need to add the trailing zero. How would I go about doing this?
You can use javascript's toFixed method (source), you don't need jQuery. Example:
var number = 258.2;
var rounded = number.toFixed(2); // rounded = 258.20
Edit: Electric Toolbox link has succumbed to linkrot and blocks the Wayback Machine so there is no working URL for the source.
Javascript has a function - toFixed - that should do what you want ... no JQuery needed.
var n = 258.2;
n.toFixed (2); // returns 258.20
I don't think jQuery itself has any string padding functions (which is what you're looking for). It's trivial to do, though:
function pad(value, width, padchar) {
while (value.length < width) {
value += padchar;
}
return value;
}
Edit The above is great for strings, but for your specific numeric situation, rosscj2533's answer is the better way to go.

How to parseInt or ParseInt embedded data with TypeScript in Qualtrics?

Even when I save an integer to embedded data earlier in the survey flow (in previous blocks on different screens), I am not able in Javascript to get the embedded data value, ensure it is parsed as a number/integer, then use it in a loop. Is this something about TypeScript? I didn't see anything about parseInt or ParseInt in the TypeScript documentation.
For example, suppose I do the following:
// Draw a random number
var x = Math.floor(Math.random() * 5);
// Save it in embedded data
Qualtrics.SurveyEngine.setEmbeddedData("foo", x);
// In a later block on a different screen, get the embedded data as an integer
var x_new = "${e://Field/foo}"; // not an int
var x_new = parseInt("${e://Field/foo}"); // doesn't work
var x_new = ParseInt("${e://Field/foo}"); // doesn't work
// Loop using x_new:
for(i = 0; i < x_new; i++) {
console.log(i)
}
Any idea why this isn't working? Perhaps I just don't know how to parseint().
In "normal" JS runtime system, we have parseInt function, the function gets a string (like number string) as a parameter. In this env, we don't support your syntax - "${e://Field/foo}", because it is not a "number string".
In Qualtrics system environment they have parseInt too, but they support their custom syntax "${e://Field/foo}" to get EmbeddedData.
Make sure that your code is running on Qualtrics system environment.
ParseInt is just turning your string into an integer.
Look at the demo below.
let myVar = "${e://Field/foo}"; // This is a string
console.log(myVar); // This prints a string
console.log(parseInt(myVar)); // This prints "NaN", i.e. Not a Number, because the string isn't a representation of a number.

jQuery: Content of html element is interpreted as a string, not an integer

When somebody is liking a comment on my website, a "1" is added at the right of the number where the amount of likes are shown, but when they click dislike, it does correct math.
For example:
14 + 1 = 141
14 - 1 = 13
jQuery
var elem = $('.like_button'), //Like button
num = $('.num_likes'), //Get the element: number of likes
oldnum = num.html(); //Number of likes
if(elem.html() == "Like") {
elem.html("Dislike");
num.html(oldnum+1); //Adds one like after liking it
} else {
elem.html("Like");
num.html(oldnum-1); //Deletes one like after disliking it
}
I really wonder why disliking works but liking not.
Why does javascript interpret the value of the num element as a string, even though it is a number? Any tips for me?
Because JavaScript interprets num.html() as text. The + sign for string in javascript means concatenation, but - doesn't mean that so in that case javascript realizes you want to do numeric calculation. That's why it works with -
You should cast oldnum to an integer with parseInt().
You need to cast oldnum to a number:
if(elem.html() == "Like") {
elem.html("Dislike");
num.html(Number(oldnum)+1); //Adds one like after liking it
} else {
elem.html("Like");
num.html(Number(oldnum)-1); //Deletes one like after disliking it
}
Alternatively, +oldnum does the same thing as Number(oldnum).
Javascript is interpreting the text on your page as a string. This is because that's what text on a page normally is. Take for example:
<span id="berliner">I am a jelly donut.</span>
<script LANGUAGE="Javascript">
document.getElementById("berliner").innerHTML;
// it only makes sense that this be a string, right?
</script>
Now, in JS, you use the + sign for two things: adding numbers, or putting one string after another.
var addingnumbers = 1+1;
// adding numbers, what you want
var a = "I am";
var b = " a jelly donut";
var addingstrings = a+b;
// adding strings, which you don't want.
As such, the html was interpreted as a string like it normally should be, but in this case shouldn't be. And adding the string to the other string just appended it to the end, rather than doing math. There is an easy solution: convert the innerHTML to a number by multiplying it by 1. Multiplying can't be done to a string, so JS will change it to number form, prepping it to be added to something else.
var oldnum = num.html()*1; // done! The multiplying has changed it to a number.
And if you ever do want to change it back to a string, you can do the reverse with the toString() function.
var aNumberToStartOutWith = 3;
var aStringToEndOffWith = aNumberToStartOutWith.toString();

I need help writing a basic return decimal from string function in Javascript

I have scoured the web for an hour-and-a-half looking for a working, simple string-to-decimal function, and have been depressingly failed. The mentioned string is comprised of letters and numbers. This is meant for a cipher. I would like something like this...
function ConvertDec(str){
//do conversion
return decVal;
}
Were it would be used as such...
decSeed = ConvertDec(seed);
decUserString = ConvertDec(strVal);
These values would then be multiplied to get a full value, which would later be decrypted.
Here's the basics
var str = "123.45";
var dec = parseFloat(str);
console.log(dec); // 123.45
Read more about parseFloat here

Once value increments digits stops working

I have a function so say i have a list:
1,2,3,4,5,6,7,8,9
and if i click say number 3 , number 3 gets removed and all the values larger than this value will be taken away by one so it'd end up looking like this:
1,2,3,4,5,6,7,8
The only problem is that once we start going into the tenths the function just does not work for example if i had:
8,9,10,11,12,13
its meant to look like if i took 9 like:
8,9,10,11,12
but it goes like this:
8,10,11,12
completely ignorning once we get to double figures.... heres what i have:
$("button").each(function(){
if (pad2(cl_on)<pad2($(this).val())){
$(this).val($(this).val()-1);
}
});
i did have a fix for this going upto 99 but once i go over to 100 onwards the same problem resurects, this is the function i have to fix it upto 99...
function pad2(number) {
return (number < 10 ? "0" : "") + number }
There must be something wrong with even having to use this function... can someone help me so i dont have this problem again say if i reach 1000 or 10000?
Convert the strings to numbers, e.g. using unary +. You can also simplify your code to:
$("button").val(function(i, val) {
return +cl_on < +val ? val - 1 : val;
});
As I said in my comment, you are comparing strings, and in lexical(?) order, 10 comes before 2 for example.
What Felix said, or use parseInt() to achieve the same;
$("button").each(function(){
if (parseInt(cl_on, 10) < parseInt($(this).val(), 10)){
$(this).val(parseInt($(this).val(), 10) - 1);
}

Categories