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.
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:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);
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 +.
This question already has answers here:
javascript calculation formula is not working
(2 answers)
Closed 10 years ago.
So here is a dorky experiment I put together basically trying to generate a D&D style attack roll with a modifier. I want to add the numbers, but javascript keeps adding the numbers as strings. I'm not sure how to get the basic math done..... Here is my code-
function battle()
{
var CS = document.battleForm.playerCS.value;
var D20 = Math.ceil(Math.round(Math.random() * 20))
var attackRoll = CS + D20
if (isNaN(CS))
{
alert ("please provide your Combat Score!")
return
}
if (CS != '')
{
document.battleForm.enemyCS.value = attackRoll
}
}
To ensure two numbers are added together, try:
var num3 = +num1 + (+num2);
This could be preferred over the use of parseInt or parseFloat for two reasons:
+ will convert any number (meaning, you don't need a different method for an integer and a float)
+ will fail if either value are not convertible. parseInt and parseFloat ignore any trailing text in the variable. So for example, parseInt("10px", 10) results in 10, while (+"10px") results in NaN.
It's up to you what you want to use.
var CS = parseInt(document.battleForm.playerCS.value, 10); // or parseFloat if you expect float number
The value of any form text is a string, so you need to convert it to number.