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()
– Rafael Augusto
Weird! I copied and pasted your example in jsfiddle and look at the result: https://jsfiddle.net/9cvcz414/
– ISFO
@ISFO is that in my case some values I’m adding via text() inside the tag. I think that might be my problem.
– user27585
@Erick use parseFloat() like I said
– Rafael Augusto
@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
– user27585
I edited the code by taking the text from a span
– Rafael Augusto
Did Erik see my answer? If you use
.toFixed(2)
as I suggested, you can transform10.5
in10.50
.– Sergio