Solved

Dividing a Variable in Javascript Returns NaN?

Posted on 2009-05-11
16
1,554 Views
Last Modified: 2012-05-06
I have a calculated variable in a .js file, let's call it "Total" and I'm trying to divide it, prior to making the calculation, the division appears to be working fine. But once I run the calculation and attempt to divide the result of the "Total" variable, I get a return of NaN.

I know that this is the standard 'Not a Number" return, which confuses me seeing how the "Total" variable is defnitely and number and should therefore be divisible.

Any ideas?

Here's a cut of the code used:

"<td align=\"center\"><b>" + divide(mdRecTotalBtuh, 2) +" </b></td>" +

which calls.

function divide($a, $b )
{
   return ($a/$b);
}
0
Comment
Question by:deltex141
  • 8
  • 8
16 Comments
 

Expert Comment

by:ideazinfinite
ID: 24357838
try typecasting variables to integer or float depending on your inputs
0
 

Expert Comment

by:ideazinfinite
ID: 24358893
My answer was definitely the solution.
0
 
LVL 4

Author Comment

by:deltex141
ID: 24359250
After further work, it appears that as long as the variable is under 1000, the code works, however, once it's over, the NaN error appears. Which leads me to believe that the result of the divison has to be rounded to 2 decimal places (or less) to avoid the NaN error message.

That is what I need to know, how to integrate the round into the division function
0
 
LVL 4

Author Comment

by:deltex141
ID: 24359318
I figured out how to round it, still get the NaN error...
0
 

Expert Comment

by:ideazinfinite
ID: 24359319
try round method.. that is round(variable)
0
 

Expert Comment

by:ideazinfinite
ID: 24359322
or Math.round(var) in some cases
0
 
LVL 4

Author Comment

by:deltex141
ID: 24359421
How do i typecast a variable inside a javascript function?
0
 

Expert Comment

by:ideazinfinite
ID: 24359528
use the functions: Number(), Boolean() or String() to convert.. for example:

var n = 50; // number

var b = Boolean(n); // b is "true"
var c = Boolean(n - 50); // c is "false"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Author Comment

by:deltex141
ID: 24359542
Can you clarify? I'm currently using this script to perform the divison and reduce the decimal count to 2

function divide($a, $b, X) {
// rounds number to X decimal places, defaults to 2
z = ($a/$b);
X = (!X ? 0 : X);
return Math.round(z*Math.pow(10,X))/Math.pow(10,X);
}
0
 

Expert Comment

by:ideazinfinite
ID: 24359600
Try printing z after u compute it and see whether it gets the division right or wrong. try that for every variable.. and the point where u get that Nan, try convert it to string() and return.
0
 
LVL 4

Author Comment

by:deltex141
ID: 24359701
The division works fine until the $a variable goes over 4 digits, should i be converting it prior to the divide function or inside it?
0
 
LVL 4

Author Comment

by:deltex141
ID: 24359704
$a is the variable that has to be converted
0
 

Expert Comment

by:ideazinfinite
ID: 24359721
I would convert it prior to the division but then again, for the division you have to make sure that the $a is an integer that is use Number() function again to convert it back to the integer for division.

So if its four digits, round it, and then divide
0
 
LVL 4

Author Comment

by:deltex141
ID: 24367105
I've updated the code to convert the variables used (both of them) to numeric, it has not helped, here is the code used

function divide($a, $b, X){
// convert to numeric by multiplying values by 1.0
c = $a * 1.
d = $b * 1.
e = c / d
X = (!X ? 2 : X);
return Math.round(e*Math.pow(10,X))/Math.pow(10,X);

}
0
 

Expert Comment

by:ideazinfinite
ID: 24367574
How about doing it this way..
function divide($a, $b, X){

// convert to numeric by multiplying values by 1.0

c = Number($a)

d = Number($b)

e = c / d

X = (!X ? 2 : X);

return Math.round(e*Math.pow(10,X))/Math.pow(10,X);
 

}

Open in new window

0
 
LVL 4

Accepted Solution

by:
deltex141 earned 0 total points
ID: 24367668
I solved the problem, it was not the coversion that was the problem.

The issue was that a number over 1000 would contain a comma, I had to code to remove the comma prior to the calculation.

Case Closed
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now