Add values with jquery, string to number

Asked

Viewed 1,903 times

1

I’m trying to add some values but instead of a result of the sum I’m getting the values concatenated, 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 then convert to R$ and present as R$ 10.50.

How do I turn this concatenated value into the correct sum value?

You should use parseInt something like var d = parseInt(a+b+c) but it didn’t turn out the way I’d hoped.

Update Obs.: In my case some values I am adding as text() inside the tag, in some cases I am taking the date('value') of the tag and adding the value inside it via text(), there is some other way that keeps the number as number and not string?

  • use parseFloat()

  • Weird! I copied and pasted your example in jsfiddle and look at the result: https://jsfiddle.net/9cvcz414/

  • @ISFO is that in my case some values I’m adding via text() inside the tag. I think that might be my problem.

  • @Erick use parseFloat() like I said

  • @Rafaelaugusto testing here, gave an improved but I’m still not getting the correct value of the calculation, but maybe I was wrong in the account, giving a revised. Thank you

  • I edited the code by taking the text from a span

  • Did Erik see my answer? If you use .toFixed(2) as I suggested, you can transform 10.5 in 10.50.

Show 2 more comments

2 answers

1


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>
<span id="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 function parseFloat() 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>

  • How do I take this result and keep 2 decimal places, because this value I will have to show in R$.

  • I updated the code

  • @Erick added to the code the question of decimals

  • I’m trying to adapt my code here, I think it’s in the way. Had worked out the sum giving 10.5 now trying to put in R $ using its function in the first attempt gave ,18 rs .. but I think it’s in the way ... which is the best way to insert the result in the div to using text(); ta right?

  • This yes, you can use Text without problems . text('R$ '+valueTotal)

  • So it’s right $(".value_per_student"). text('R$ '+formatReal(y)); pq can’t make 10.5 turn 10.50 ...

  • @Erick updated the code

  • Blz your code helped quite I’m not managing to make my final result turn 10.5 to 10.50 for example ... I’ll poke around more here thank you very much.

Show 3 more comments

1

You’re using numbers in format String, 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);

  • How to turn minutes understood the toFixed, it leaves with 2 houses after the . Thank you.

  • 1

    @Erick updated the answer. Using toLocaleString is more accurate.

  • Okay was cool, would it be possible to transform from a dollar format to R$ ? Pq here in Brazil instead of 10,000.00 we use 10,000.00 Thank you

  • 1

    @Erick yes, that’s the idea of .toLocaleString I changed the answer to use 'pt-BR'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.