5 Replies Latest reply on Apr 24, 2006 9:33 AM by Newsgroup_User

# change a negative number to a postive

Hi all,

Is there an easy way to change a negative number into a positive? (i.e.
"-4" becomes just plain old "4")? I've been using a complicated system of
multiplying the number by itself and then finding the square root, but Flash
has to have something easier. Any ideas?

Thanks.

• ###### 1. Re: change a negative number to a postive
Brock,

> Is there an easy way to change a negative number into
> a positive? (i.e. "-4" becomes just plain old "4")?

var n:Number = -4;
trace(n); // output: -4
n *= -1;
trace(n) // output: 4

> I've been using a complicated system of multiplying the
> number by itself and then finding the square root, but
> Flash has to have something easier.

Just multiply by -1, and you're in.

David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."

• ###### 2. Re: change a negative number to a postive
>> I've been using a complicated system of multiplying
>> the number by itself and then finding the square root,
>> but Flash has to have something easier.
>
> Just multiply by -1, and you're in.

P.S.

Of course, Math.abs() will do the same (absolute value), but the -1
approach means you can convert back to a negative number again.

David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."

• ###### 3. Re: change a negative number to a postive
y=(-4)
x = (-y)
trace(x)

//outputs 4

--

Dan Mode
*Flash Helps* http://www.smithmediafusion.com/blog/?cat=11

"Brock" <bramsay@inlets.net> wrote in message
news:e2it0h\$off\$1@forums.macromedia.com...
> Hi all,
>
> Is there an easy way to change a negative number into a positive? (i.e.
> "-4" becomes just plain old "4")? I've been using a complicated system of
> multiplying the number by itself and then finding the square root, but
> Flash has to have something easier. Any ideas?
>
> Thanks.
>

• ###### 4. Re: change a negative number to a postive
Havn't seen one.. but I ave needed to use one before and did it this way:

lets suppose a = -4
make a new var 'b'
make b = bX2 (in this case it would = -8 )
make a = a-b (a now = 4)

but for my purpose, I also had to wrap it in an if/else statement (eg, if a <0) cos I allways needed it to be a positive number or else it would turn a positive into a negitive...

Hope you find an easier way tho.... good luck.....
• ###### 5. Re: change a negative number to a postive
Wow - thanks everyone!

I'm going to use David's Math.abs() solution because I don't want it to ever
show up as a negative again (And I'm starting with a list of both positive
and negative numbers that all need to be turned to positive).

Again, though, thanks to everyone for the suggestions!

Cheers.

"Brock" <bramsay@inlets.net> wrote in message
news:e2it0h\$off\$1@forums.macromedia.com...
> Hi all,
>
> Is there an easy way to change a negative number into a positive? (i.e.
> "-4" becomes just plain old "4")? I've been using a complicated system of
> multiplying the number by itself and then finding the square root, but
> Flash has to have something easier. Any ideas?
>
> Thanks.
>