Add values with jquery, string to number

1

I'm trying to add some values but instead of a sum result I'm getting the concatenated values, follow the example:

var a = 7.5 
var b = 1.00 
var c = 2.0
var d = a+b+c 

console.log(d) = 7.51.00.2.0

But I wanted to get the following value: 7.5 + 1.00 + 2.0 = 10.5 to later convert into R $ and present as R $ 10.50.

How do I convert this concatenated value to the correct sum value?

I should use parseInt something like var d = parseInt(a+b+c) but it did not go the way I expected.

Update Note: In my case some values I'm adding as text () inside the tag, in some cases I'm getting the value of the tag and adding the value inside it via text () is there any other way that keeps the number as number and not string?

    
asked by anonymous 23.05.2017 / 15:07

2 answers

2

function formatReal(int){
        var tmp = int+'';
        tmp = tmp.replace(/([0-9]{2})$/g, ",$1");
        if( tmp.length > 6 )
                tmp = tmp.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");

        return tmp;
}

let a = document.getElementById('a').textContent;
let b = document.getElementById('b').textContent;
let c = document.getElementById('c').textContent;

let result = parseFloat(a) + parseFloat(b) + parseFloat(c);

$('#result').text('R$ '+formatReal(result));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><spanid="a">10.5</span>
    <span id="b">18.9</span>
    <span id="c">45.8</span>
    
    <div id="result"></div>

function formatReal(int){
        var tmp = int+'';
        tmp = tmp.replace(/([0-9]{2})$/g, ",$1");
        if( tmp.length > 6 )
                tmp = tmp.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");

        return tmp;
}

let a = document.getElementById('a').textContent;
let b = document.getElementById('b').textContent;
let c = document.getElementById('c').textContent;

let result = parseFloat(a) + parseFloat(b) + parseFloat(c);

alert(result)
alert(formatReal(result));
<span id="a">10.5</span>
    <span id="b">18.9</span>
    <span id="c">45.8</span>

let a = 1;
let b = 1000;


alert(a + b);

alert(parseFloat(a) + parseFloat(b));

Follow the example

function parseInt() parses a string argument and returns an integer in the specified base.

The parseFloat() function parses a string argument and returns a floating point number.

let a = document.getElementById('a').textContent;
let b = document.getElementById('b').textContent;
let c = document.getElementById('c').textContent;

alert(parseFloat(a) + parseFloat(b) + parseFloat(c));
<span id="a">10.5</span>
    <span id="b">18.9</span>
    <span id="c">45.8</span>
    
23.05.2017 / 15:09
2

You are using numbers in String format, you need to convert to Number .

A suggestion, using a function that allows N arguments:

var a = 6547.5;
var b = 1345354.0054;
var c = 2.0;


function somar() {
  var numeros = [].map.call(arguments, Number);
  var total = numeros.reduce(function(soma, nr) {
    return soma + nr;
  }, 0);
  return total.toLocaleString('pt-BR', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  });
}

var d = somar(a, b, c);
console.log('R$ ' + d);
    
23.05.2017 / 15:22