PDA

View Full Version : convert a number


lookatme0128
11-13-2007, 08:41 PM
i just need a quick tip....

how would i convert a negative number varible into a positive one??

ex:

var myNum:Number = -3

how can i use actionscript to make that a positive 3.....keep in mind it will be arandom number everytime so i can't just add 6....there's got to be a method somewhere that i'm missing....

Thanks

mother
11-13-2007, 08:45 PM
myNum = Math.abs(myNum);

abs stands for Absolut Value (http://en.wikipedia.org/wiki/Absolute_value)

blackelement
11-13-2007, 08:48 PM
2 ways

First method is to use the math library's absolute value method. It takes any number negative or positive and returns it as positive
Math.abs(mynumber)

Second method negate the sign if number is less than 0
if(mynumber < 0) {
mynumber = -myNumber;
}
and no you cant say mynumber -= mynumber since it will interpret it as mynumber = mynumber - mynumber

lookatme0128
11-13-2007, 09:01 PM
that worked awesomely!!! Math.abs(mynumber) is what i used....

thanks, my math skills lack and i knew there was an existing method that would work...i just forget my math terms :)

thanks again