Adding Two Numbers Concatenates Them Instead of Calculating the Sum

Adding two numbers concatenates them instead of calculating the sum

They are actually strings, not numbers. The easiest way to produce a number from a string is to prepend it with +:

var x = +y + +z;

javascript (+) sign concatenates instead of giving sum?

Here value gives you a string, hence the concatenation. Try parsing it as an Number instead:

var sum = parseInt(numberOne) + parseInt(numberTwo);

See the demo fiddle.

Adding two numbers concatenates them instead of calculating the sum node.js in amazon alexa skill

I would suggest you using AMAZON.NUMBER as slot type. I think that will work.

If that built-in slot is not suitable for any reason your needs then you can do Number(<slot_value>) to convert the string to a number.

I hope this helps :D
Cheers

Javascript: + sign concatenates instead of giving sum of variables

you are dealing with strings here, and math operations on strings will mess up. Remember when ever you are doing math operations you have to convert the data into actual numbers and then perform the math.

Use parseInt() more Details here

Your code should change to

Vfinal = parseInt(Vinitial,10) + parseInt(acceleration,10) * parseInt(time,10);

Edit 1: If the numbers are decimal values then use parseFloat() instead

So the code would be

Vfinal = parseFloat(Vinitial) + parseFloat(acceleration) * parseFloat(time);

sum of two array in Node.js

A contains numbers while B contains strings. You want to cast to numbers first to make the computation, and then cast the result to string to get a string; you can use .toFixed to get a given number of precisiong (according to your expected output).

    A = [
25.8, 27,
24.099999999999998, 30.070000000000004,
34.3, 34.300000000000004,
34.3, 33,
29.2, 29.2,
27.6, 28.999999999999996,
29.310000000000002, 27.000000000000004
]

B = [
'0.00387000', '0.00472000',
'0.00534000', '0.00460000',
'0.00060000', '0.00032000',
'0.00053000', '0.00327000',
'0.00217000', '0.00217000',
'0.00460000', '0.00415000',
'0.00852000', '0.02241000'
]

C = A.map((a,i)=> (a+Number(B[i])).toFixed(8))
console.log(C)

Javascript 2 Decimal Float Concats Instead of adding

Number.prototype.toFixed() return string as response. Adding a string to a number will typecast the result to string.

So you should set total as

var total = parseInt(servicefee) + +parseFloat(cost).toFixed(2);

The + symbol infront of +parseFloat(cost).toFixed(2); will convert the string to number.

Or else you can perform the addition and add .toFixed(2) to the total to keep the decimal places in the final result

Working Fiddle

function update() {
var servicefee = 1300;
var select = document.getElementById('select-visa');
var cost = select.options[select.selectedIndex].value;
var total = parseInt(servicefee) + parseFloat(cost);
document.getElementById('total').innerHTML = total.toFixed(2);
}
<select id="select-visa" class="selectpicker "  onChange="update()" aria-label="Default select example" >
<option value="1725.52">Example 1</option>
<option value="200">Example 2</option>
</select>

<p id="total"></p>

Adding two numbers in JavaScript incorrectly

This might happen because they are strings. Try parsing them:

Global.alert(
"base: " + base + ", upfront: " + upfront + ", both: " +
(parseInt(base) + parseInt(upfront))
);

If those numbers are decimal you will need the parseFloat method instead.

Javascript (+) sign concatenates instead of giving sum of variables

Use this instead:

var divID = "question-" + (i+1)

It's a fairly common problem and doesn't just happen in JavaScript. The idea is that + can represent both concatenation and addition.

Since the + operator will be handled left-to-right the decisions in your code look like this:

  • "question-" + i: since "question-" is a string, we'll do concatenation, resulting in "question-1"
  • "question-1" + 1: since "queston-1" is a string, we'll do concatenation, resulting in "question-11".

With "question-" + (i+1) it's different:

  • since the (i+1) is in parenthesis, its value must be calculated before the first + can be applied:

    • i is numeric, 1 is numeric, so we'll do addition, resulting in 2
  • "question-" + 2: since "question-" is a string, we'll do concatenation, resulting in "question-2".


Related Topics



Leave a reply



Submit