Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
var a = $('.box').width();
var b = $(document).height();
Now, let's say 124px, is an int or string ?
So far I have done this through trial and error, but now I'm curious to understand why if (b < 20), however parseInt is needed to calculate var c = a + b. why is that?
Note: I'm assuming that a and b are strings like "10" and "20", for the purposes of this question. $('.box').width() will actually return 123 (if your element was 123 pixels wide, for example), so in this case parseInt is irrelevant.
Because in
a + b
If you don't use parseInt, then JavaScript will assume you want to concatenate the strings (because + is overloaded for concatenation). However, when you use
b < 20
JS knows that you can't have "a string less than 20," because that makes no sense, so it casts to a number automatically.
We don't.
.width() and height() return numbers, not strings, and so don't need to be converted using parseInt(...,10). I don't know who the "we" is you refer to, but it does not include people who use jQuery correctly.
Try it out on this very website - open up a console, and type var d = jQuery("div"); console.log(typeof d.height(), typeof d.width(), d.height() + d.width());, and you'll see [number], [number], and the result of a normal numeric addition.
because when you use the < operators it only makes sense for numbers to be bigger or smaller, however for the "+", in javascript it could be used to concatenate strings as well so:
"30"+"20"="3020"
and 30+20=50
NOTE: #Mike Poxman Kamerman is right, for the most part it will work without parseInt because the width and height jquery functions return floats but it is the correct practice as javascript used to be very strict and in browsers like older IEs it could not work.
When you are comparing a string and a number, one has to be converted to the type of the other for the comparison to be possible. The string will implicitly be converted to a number.
When you are using the + operator with two strings, there is no conversion needed for the operation to be possible. It will just concatenate the strings, but if you want to use the operator for addition instead of concatenation, then you need to explicitly convert the strings to numbers first.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert a 17digit number string into a number
this is the number "76561197962169398".I tried using parseInt()
The result of using parseInt is :-
76561197962169390
I am loosing the last digit.I also tried BigInt() 'n' is getting appended to the number.
I m thinking of using replace() with a regex for only digits.
Is there any other way I can achieve this without loosing precision.
Please any help regarding this is really appriciated.THANK YOU
in chrome 83 devtools:
x=76561197962169398n
76561197962169398n
++x
76561197962169399n
typeof x
"bigint"
y=BigInt("76561197962169398")
76561197962169398n
++y
76561197962169399n
x+y
153122395924338798n
x + 1
VM342:1 Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
at <anonymous>:1:2
(anonymous) # VM342:1
x + 1n
76561197962169400n
[5n, 3n, 9n, 7n].sort()
[3n, 5n, 7n, 9n]
The n suffix is for display - and in code it's needed to say a literal value needs to be treated as bigint instead of number - think of it like quotes for strings - without quotes a sequence of characters is not a string - similarly a number without n suffix is not a bigint - it's a number that has limited precision and simply cannot be used for large values
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a equation in the form of string like this 1+(x*10) = 21 . I have to covert it like this x = (21-1)/10 in any language . I do not want an exact solution but Please give me some hint .
If a hint is enough for you check out this library:
http://algebra.js.org/
It is capable of taking definitions of equations (as string or step by step), solve them and make the result available as string.
Easiest way - call some CAS (computer algebra system) program from command line passing equation to solve. Like:
maxima --very-quiet -r 'solve ([1+(x*10) = 21], [x]);'
And get results back from standart output. But of course this should be done server-side,
i.e. if from PHP - can be executed with shell_exec() function.
To me, this problem can be divided into 2 stages, finding x's side (left or right of the "="), and scanning and converting a literal along with the appropriate operation.
For the first stage, you can find which part of the equation x lies in by simply comparing the indexes of "=" and "x" in the String.
There are 2 types of components you need to worry about now i.e. operands (values) and operations (+, - etc) .
You can try shifting operands from the "x-side" to the other, by simply converting the operation associated with the value (+ becomes - and vice versa, same for * and /). For example, 1 + x = 7. 1 is the operand and '+' is the operation associated with 1, which gets converted to '-' when it moves to the other side.
Do make sure to add a bracket at the start and end of the right hand side every time you move an operand and operation to the other side, so that the equation maintains the order of calculations needed to be done.
Hope this helps!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
As you can see from the title I have various cases for strings that can contain numbers in them. I found out that using parseInt() and parseFloat() didn't work for me as parseInt will convert number like 10.28 to just 10, but parseFloat will make number like 10 into 10.0, I want to somehow convert string into number so it stays exactly like it was in the string without anything removed or added.
Per MDN Number ( MSDN page also, but not so much info ).
At the top of the page:
The primary uses for the Number object are:
If the argument cannot be converted into a number, it returns NaN.
In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.
At the bottom of the page, there are some examples:
Convert numeric strings to numbers
Number("123") // 123
Number("") // 0
Number("0x11") // 17
Number("0b11") // 3
Number("0o11") // 9
Number("foo") // NaN
Number("100a") // NaN
Demo https://jsfiddle.net/hxkfafdw/
More on the topic - Number("foo") is NaN Number("f00") - same. Number("0xf000") - this is a hex number.
This question already has answers here:
Javascript concatenating numbers, not adding up
(3 answers)
Closed 7 years ago.
var tt = gas+0.1
document.write (vartt);
Duplicate
You could make use of Number function too.
var tt = Number(gas) + 0.1;
document.write(tt);
The user entered a string. If you want to do arithmetic with it instead of string concatenation, you must convert to a number. There are many different ways to do that including parseInt(gas, 10), parseFloat(gas), Number(gas) and +gas:
Here's one implementation:
var tt = parseFloat(gas) + 0.1;
document.write(tt);
Also, your document.write() statement was not correct either. The variable name is just tt, not vartt.
Unless you are using <input type="number" /> for the input, the user provided data will be a string. By default, when you try to add a string + a number it will cast that number to a string. You can do what Видул Петров suggested and add the unary + to gas to force cast it to a number, however if it's still a string that can't be cast to a number (like someone entering in the word 'five' vs '5'), youll get NaN as a result unless you have the proper control over the incoming data.
What's the difference between
"2" * 1 + 5
and
parseInt("2") + 5
It's just a to write a more readable code, or there are compatibility issues with the first form.
parseInt is used to grab integers from a string. Consider the following code:
var myString = "3 blind mice";
var myInteger = parseInt(myString); //3
JavaScript will do automatic type conversion, so something like this:
"2" * 1 + 5 //7
The string "2" gets converted to a number.
As noted above in the comments, parseInt takes an additional argument for the base.
JavaScript has a lot of very weird rules about type conversion, and sometimes it's not exactly clear what JavaScript will do in every situation. Keep in mind that the + operator is also used for concatenation as well as addition.
If you're trying to explicitly convert something to a number, you can use the Number constructor provided by JavaScript. Considering the following:
var myString = "2";
var myNum = Number(myString); //2
console.log(typeof myNum); //number
Without the new keyword, it can be used to convert strings to numbers. While it does work, I am not sure parseInt should be used for conversion. Just use the Number constructor.
I don't think there are compatibility issues with using coercion in the first form, it is a language feature that should be widely supported.
However, since you have to add code to do the conversion either way (* 1 vs. parseInt), I would vote for parseInt from a style perspective because it makes your intention clearer. It is hard enough sometimes to keep types straight in javascript without using implicit conversions.
Someone not familiar with your code or with javascript might wonder what is going on with that first form as well.
Plus, as indicated in the comments, parseInt is faster so it's a win all around.