2
//for example 
allInputs[22].value //equals $45 and
allInputs[4].value  // equals $70

allInputs[22].value + allInputs[4].value = "$45$70"

It equals "$45$70". But I only want to sum up the value. how do I sum up both values to get the final value ignoring the dollar sign?

Joshua
  • 40,822
  • 8
  • 72
  • 132

7 Answers7

5

You can use

"$" + (parseInt(allInputs[22].value.substring(1)) + parseInt(allInputs[22].value.substring(1)))

The substring method, will get rid of the $ sign, and parseInt will convert it to a number. You need that, because if you do not use that it will concatenate the values as strings. Note that i put another set of brackets to sum the numbers. That is because, when the interpreter sees the "$" it thinks it should concatenate strings. But we want to sum the numbers and then concatenate the sum with the "$" string.

Andy
  • 61,948
  • 13
  • 68
  • 95
kkica
  • 4,034
  • 1
  • 20
  • 40
  • 2
    If you're going to use `parseInt` make sure to include the radix value: `parseInt(str, 10)`. – Andy Aug 20 '17 at 19:30
2

You can use reduce and check for a non-number sign at the beginning of a value:

var allInputs = ["$45","$70"];
var sum = allInputs.reduce(function(pre, curr){
  if(isNaN(curr[0]))return pre+(+curr.slice(1));
  return pre+curr;
},0);

console.log('$'+sum);
Blauharley
  • 4,186
  • 6
  • 28
  • 47
  • This really only works if the inputs are consecutive. In the example the inputs are the 22nd and the 4th. – Andy Aug 20 '17 at 19:21
2

This is a general function expression that accepts the string value from a form input and returns a number.

const getNumber = (val) => Number(val.match(/[\d\.]+/));

You can use it like this:

const sum = getNumber(allInputs[22].value) + getNumber(allInputs[4].value);

DEMO

Note: ideally you should store the currency value ($, £, € etc) separately from the values so this doesn't become an issue.

Andy
  • 61,948
  • 13
  • 68
  • 95
1

I guess you need parseFloat(). Accordingly the following would be my helper function.

function addDollars(s1,s2){
  var n1 = parseFloat(s1.replace(/[^0-9\.]/g,"")),
      n2 = parseFloat(s2.replace(/[^0-9\.]/g,""));
  return "$"+ (n1+n2).toFixed(2);
}

console.log(addDollars("$123.42","$12.88"));
Redu
  • 25,060
  • 6
  • 56
  • 76
0

You can use

parseFloat(allInputs[22].value.slice(1)) + parseFloat(allInputs[4].value.slice(1))

Remember that string are arrays. if you want to end up with the "$" sign then just concatenate it.

MarioE
  • 174
  • 9
0

You need to remove the "$" and convert the strings into numbers to sum them up.

Code

You can remove the "$" with replace like allInputs[22].value.replace('$', '') this will return "42" as a string.
Now we need to convert this string into a number. There are many ways to do this. I use Number() in the following solution.

Solution

var someMoney = '$50'
var moreMoney = '$60'

var toMuchMoney = "$" + Number(someMoney.replace('$', '')) + Number(moreMoney.replace('$', ''))

console.log(toMuchMoney)
Roman
  • 4,922
  • 3
  • 22
  • 31
0

To solve this, You should know about the difference between concatenation and addtion in javascript.

If you add two strings, You get concatenation of both strings as answer

"$45" + "$70" = "$45$70"

If you add two integers, you get addition.

45 + 70 = 115

So, to solve your problem, You need to first extract numbers from your variables and then do addition on them. To extract numbers you can use any method but I am using split. To convert string into integer you can use parseInt

let num1 = "$45";
let num2 = "$70";


function getValue(num) {
  return parseInt(num.split('$')[1]);
}

let sum =  getValue(num1) + getValue(num2);

console.log("$" + sum);
Anurag Singh Bisht
  • 2,683
  • 4
  • 20
  • 26