Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Negative Numbers

Reply
Thread Tools

Negative Numbers

 
 
Marston A.
Guest
Posts: n/a
 
      07-13-2006
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Justin Collins
Guest
Posts: n/a
 
      07-13-2006
Marston A. wrote:
> 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

 
Reply With Quote
 
 
 
 
gekkonier
Guest
Posts: n/a
 
      07-13-2006
-12345.abs

Look at this: http://www.ruby-doc.org/core/classes/Fixnum.html


Marston A. schrieb:

> 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.
>
> --
> Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
Mat Schaffer
Guest
Posts: n/a
 
      07-13-2006
On Jul 13, 2006, at 12:19 PM, Marston A. wrote:
> 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

 
Reply With Quote
 
Craig Kim
Guest
Posts: n/a
 
      07-13-2006
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" <(E-Mail Removed)>
To: "ruby-talk ML" <(E-Mail Removed)>
Sent: Thursday, July 13, 2006 12:32 PM
Subject: Re: Negative Numbers


> On Jul 13, 2006, at 12:19 PM, Marston A. wrote:
>> 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
>
>
>


 
Reply With Quote
 
Scott Hill
Guest
Posts: n/a
 
      07-13-2006
You can also call the abs method of a number to get its absolute
value, which will be positive.

--Scott

On 7/13/06, Craig Kim <(E-Mail Removed)> wrote:
> 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" <(E-Mail Removed)>
> To: "ruby-talk ML" <(E-Mail Removed)>
> Sent: Thursday, July 13, 2006 12:32 PM
> Subject: Re: Negative Numbers
>
>
> > On Jul 13, 2006, at 12:19 PM, Marston A. wrote:
> >> 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
> >
> >
> >

>
>



--

--Scott

 
Reply With Quote
 
Marston A.
Guest
Posts: n/a
 
      07-13-2006
Hey everyone,

Great, this is exactly what I need. Thanks for the info.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating Random Numbers between a potentially negative range laura.paterson@gmail.com Java 3 02-09-2006 09:04 AM
Negative setup and Negative hold prem_eda VHDL 5 10-11-2004 12:14 PM
Negative Numbers? Charles A. Lackman ASP .Net 4 09-28-2004 12:27 AM
Replace negative numbers with 0 in a file Mayank Perl 2 11-28-2003 11:08 AM
negative numbers and integer division Matthew Wilson Python 7 10-03-2003 09:53 PM



Advertisments