This question already has answers here:
numerical value of number input by user in a text field
(5 answers)
Closed 9 years ago.
Well i was trying to create a simple calculator using HTMl, JS and JQuery ,
I created two text box for input of numbers but ,
I am not able to add the given numeric values in the input box , it always concatenate the values . Please Help Me .
when you read the values from your input boxes, you are reading them as strings. unfortuneately for javascript & math, the + symbol means two different things (in math it means add, in js is means concatentate, and, well add sometimes too).
You need to cast your strings as ints ( or floats or whatever) and then add them in javascript. you can do that this way:
var intstr0 = "1";
var intstr1 = "2";
var sum = parseInt(intstr0) + parseInt(intstr1);
Input boxes values' will be strings, and not numbers. To convert that input box to a number, try parseInt() or parseFloat() around the values. Then, you can add them with +.
Related
This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
So im quite new to javascript and i tried making something simple as entering a value and add 150 to it but it wont show number + 150?
Pictures below
Code
Output
That's because you are concatenating strings. You need to convert the strings to integers by using parseInt(), and then add the numbers.
https://www.w3schools.com/jsref/jsref_parseint.asp
var fullprice = parseInt(price) + 150;
Because you're actually concatenating a string with a number. The input value is a string, so before operating with it, parse it to int with parseInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
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.
This question already has answers here:
Sum of two input value by jquery
(7 answers)
Closed 7 years ago.
I have three inputs that need to be calculated. I used the .val() method in jQuery to get the value of the input and calculate them with following:
$("document").ready(function(){
$("#calculate").click(function(){
document.getElementById("result").innerHTML="$"+($("#einc").val()+$("#iinc").val()-$("#slint").val());
$("#result").css("display","inline");
});
The first two inputs don't add up, they concatenate; i.e. 5+5=55.
However, the subtraction works fine. So if my first two inputs are 10 and 10 and my last input is 5, it will give me 1010-5=1005.
My expected answer should be 10+10-5=5.
What is wrong with my code?
Because .val() returns string and + also act as the string concatenation operator.
You can convert the string value to a numeric one using parseInt()/parseFloat()/unary operator
$("#result").html("$" + (+$("#einc").val() + (+$("#iinc").val()) - $("#slint").val()));
try this:
Number($("#your_input_field_id").val());
jQuery val() returns string.
This question already has answers here:
Unexpected output in javascript
(5 answers)
Closed 8 years ago.
var apples = prompt('Please enter no. of apples');
var oranges = prompt('Please enter no. of oranges');
var fruits = apples + oranges;
document.write(fruits);
Why does it work with - and * and not +?
Thanks!
You're adding two strings together to get another string. That's how JavaScript does it.
Maybe what you want is numbers:
var fruits = parseInt(apples, 10) + parseInt(oranges, 10);
As a note, using prompt to collect information is utterly barbaric. What you need to do is have two input boxes and a submit trigger that does the math, or since it's so trivial, hook it up to trigger on any change to either value. jQuery basics here.
You're doing string concatenation with the + instead of an addition.
Parse to float or int.
var fruits = parseFloat(apples) + parseFloat(oranages);
prompt returns a string, thus it's appending the strings together.
Javascript will convert to an int when you try other operators on the variables.
Use parseInt to make sure they're ints.
This question already has answers here:
Javascript string/integer comparisons
(9 answers)
Closed 7 years ago.
My javascript is behaving very strange!
I have a input field in my html file. This input field is read by javascript in the following way:
var bscore = $("#bscore").val();
Then i want to show an alert if the input is below a certain input
if(bscore<"913"){
document.getElementById("bscorealert").style.display="block";
};
This works fine. So when the number is above 913 it should not show. Only javascript is behaving very strange, when the number in the bscore input field is above 999 (so 1000 and higher) the if statement is triggered and the alert is shown.
How is this possible?
It's possible because you are comparing strings, not integers. String comparison is lexicographical, so "9" > "10" and so on.
You will want to convert before comparing with Number(strValue) or with the unary operator +:
var bscore = +$("#bscore").val(); // + prefix converts to number
if (bscore < 913) ... // no quotes around 913!
You're trying to compare 2 strings, which uses the alphabetical comparison. If you enter a value between 9130 and 9999 in the field, you'll notice that the messagebox doesn't show as well.
You'll have to do a parseInt to compare numerical values.