S
S
Sergey Titov2015-10-08 02:43:20
JavaScript
Sergey Titov, 2015-10-08 02:43:20

How to sum thousandths in javascript?

I'm trying to sum through javascript with thousandths, example:
173.206 + 0.007 , for some reason I get 173.007, what could be the problem?
jsfiddle.net/tbjvcw1r/1

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2015-10-08
@Nuboskill

replace parseInt with parseFloat :
var value1 = parseFloat($(".value").text())

V
Vitaly Inchin ☢, 2015-10-08
@In4in

What does parseInt do?
• Parses the string, from the beginning, until it encounters a non-digit character in its path.
And the point is.
You have two options:
Use parseFloat , which works like parseInt , but takes fractions into account.
Or use a unary plus to convert a string to a number, but if the string is invalid, this will return NaN:

22 + +"22.18"; // 44.18
+"22lol"; //NaN

E
Evgeniy _, 2015-10-08
@GeneD88

var value1 = + $(".value").text()
var randomVal1 = function(min,max){
return Math.random() * (max - min) + min;
};
var value1 = +$(".value").text(),
rand = randomVal1(+value1 + 0.001,+value1 + 0.006).toFixed(3);
alert(rand);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question