Groups | Blog | Home
all groups > flash actionscript > july 2004 >

flash actionscript : How do you round correctly?



Azeral
7/13/2004 10:22:04 PM
Okay i looked through the actionscript dictionary and found that
math.round(mynumber) rounds down.

But how do you use it correctly for example:

Point = math.round(nAdvertisingPoint) /10;
trace(Point);

But it gives me a strange error saying : NaN

How do i fix this? im new to actionscript =/ im not exactly sure what to do. I
followed the example in the actionscript dictonary pretty well.
mandingo
7/13/2004 11:44:37 PM
The the error is likely to be in your variable 'nAdvertisingPoint'

If this holds text (or something other than a number), then you will receive
NaN which represents Not a Number.

if you check that variable to ensure it is reporting as a number you shouldn't
have any other troubles... I tested the rest of that small code with :

nAdvertisingPoint = "347.9876"
Point = Math.round(nAdvertisingPoint) /10;
trace(Point);

and the output is : 34.8

I hope that helps.
cheers,
Azeral
7/14/2004 12:21:58 AM
mandingo
7/14/2004 12:37:44 AM
that shouldn't matter... the code doesn't care where the variable comes from...
only that it requires it to be a number...

try this in your code:

trace("nAdvertisingPoint contains " + nAdvertisingPoint + " and is a type of "
+ typeof(nAdvertisingPoint));

Point = Math.round(nAdvertisingPoint) /10;
trace(Point);

if you don't get back something like :
output : nAdvertisingPoint contains 348.7653 and is a type of number
then you are going to get NaN as your Point.

as I said, check what your incoming value is for nAdvertisingPoint ... the
rest of the code is correct.
AddThis Social Bookmark Button