Reputation: 3374
I have a function having following lines:
payementReceived=data.toJSON().total;
paymentTotal=$("#paymentTotal").html();
console.log(typeof(parseInt(paymentTotal)));
console.log(typeof(parseInt(paymentReceived)));
console.log(parseInt(paymentTotal)-parseInt(paymentReceived));
I get the following in console
number
number
NaN
i don't understand if both are numbers then why it isn't able to give the proper substraction result.
Upvotes: 2
Views: 110
Reputation: 5610
Just a funny idea :
if (typeof n === "number" && n+1 !== n) {
// this is a valid number
}
invalid+1 will still be invalid so it could check for invalid numbers.
Upvotes: 0
Reputation: 193
try below code
console.log(Number(paymentTotal)-Number(payementReceived));
Upvotes: -1
Reputation: 9968
problem is because of the undefined varaible used "paymentReceived" if you are sure that the result is a number you can always use parseInt
console.log(parseInt(paymentTotal)-parseInt(payementReceived));
Upvotes: 0
Reputation: 161447
The values are number
in that their contents are a number, but it doesn't mean that the number is valid.
typeof NaN === 'number'
If you are getting a NaN
back from your subtraction, one or both of your input values are invalid numbers.
Upvotes: 2