admin管理员组文章数量:1384622
I have two variables holding integer values:
x = 36,000;
y = 18,045.40;
this is how i subtract:
z = parseInt(x) - parseInt(y);
the result is 15.
If i remove the parseInt the result is 'Nan'.
How do I go about subtracting x with y without rounding off or removing thousands?
many thanks.
I have two variables holding integer values:
x = 36,000;
y = 18,045.40;
this is how i subtract:
z = parseInt(x) - parseInt(y);
the result is 15.
If i remove the parseInt the result is 'Nan'.
How do I go about subtracting x with y without rounding off or removing thousands?
many thanks.
Share Improve this question edited Dec 13, 2011 at 17:09 MacMac 35.4k55 gold badges153 silver badges224 bronze badges asked Dec 13, 2011 at 16:50 Saint DeeSaint Dee 1,0156 gold badges22 silver badges43 bronze badges 1- Don't gave mas in your value, also see this demo : Fiddle for this in which you have a idea to subtract two floating values even it is a positive or negative value... – lk.annamalai Commented Feb 21, 2013 at 5:07
3 Answers
Reset to default 6Don't put mas in your numbers.
The code you have posted won't even run. I would remend pulling the ,
s out of your numbers and using parseFloat instead. This appears to give the result you want. Demo here:
http://jsfiddle/yVWA9/
code:
var x = 36000;
var y = 18045.40;
alert(parseFloat(x) - parseFloat(y));
There is no separator for thousands in Javascript. Your variables are either holding strings and not integer values or you are getting syntax error.
If you have strings and they cannot be changed (like received from service, etc.) then try this:
x = "36,000";
y = "18,045.40";
// remove mas and convert to numbers
function norm(num) { return +num.replace(',', ''); }
console.log(norm(x) - norm(y));
本文标签: Subtraction in JavaScript only returns two digits of the thousandsStack Overflow
版权声明:本文标题:Subtraction in JavaScript only returns two digits of the thousands - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744489842a2608668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论