javascript - How do you sum two values containing the dollar sign "$"? I have been trying to figure out the me

for example allInputs[22].value equals $45 andallInputs[4].value equals $70allInputs[22].valu

//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?

//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?

Share Improve this question edited Aug 23, 2018 at 15:53 Joshua 43.4k9 gold badges78 silver badges149 bronze badges asked Aug 20, 2017 at 19:05 Elvis NgbokiElvis Ngboki 355 bronze badges 1
  • Replace $ with empty string, then parse the strings to int – JOSEFtw Commented Aug 20, 2017 at 19:10
Add a ment  | 

7 Answers 7

Reset to default 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.

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);

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 bee an issue.

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"));

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.

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)

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744036351a4547491.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信