Negative Numbers

M

Marston A.

Hey all,

I'm just getting started with Ruby and was wondering if there is a way
to translate negative numbers into their positive equals. i.e turning
-7 into 7. Thanks for any help.
 
J

Justin Collins

Marston said:
Hey all,

I'm just getting started with Ruby and was wondering if there is a way
to translate negative numbers into their positive equals. i.e turning
-7 into 7. Thanks for any help.

irb(main):001:0> -7.abs
=> 7


-Justin
 
M

Mat Schaffer

Hey all,

I'm just getting started with Ruby and was wondering if there is a way
to translate negative numbers into their positive equals. i.e turning
-7 into 7. Thanks for any help.


Or if you want to flip the sign, just prepend a '-':

irb(main):001:0> --7
=> 7

-Mat
 
C

Craig Kim

You can always multiply it by negative one (-1), e.g.

irb(main):001:0> a = -7
=> -7
irb(main):002:0> a *= -1
=> 7
irb(main):003:0> a
=> 7

----- Original Message -----
From: "Mat Schaffer" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Thursday, July 13, 2006 12:32 PM
Subject: Re: Negative Numbers
 
S

Scott Hill

You can also call the abs method of a number to get its absolute
value, which will be positive.

--Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top