[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Negative Numbers

Marston A.

7/13/2006 4:20:00 PM

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-....

7 Answers

Justin Collins

7/13/2006 4:23:00 PM

0

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

gekkonier

7/13/2006 4:24:00 PM

0

-12345.abs

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


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-....

Mat Schaffer

7/13/2006 4:33:00 PM

0

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

Craig Kim

7/13/2006 5:28:00 PM

0

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" <schapht@gmail.com>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
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 Hill

7/13/2006 5:39:00 PM

0

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 <cjkim@jcnow.com> 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" <schapht@gmail.com>
> To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
> 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

Marston A.

7/13/2006 5:41:00 PM

0

Hey everyone,

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

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

Bertram Scharpf

7/13/2006 8:13:00 PM

0

Hi,

just another comment:

Am Freitag, 14. Jul 2006, 01:22:56 +0900 schrieb Justin Collins:
> Marston A. wrote:
> >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

Be careful. The minus sign is part of the constant. A minus
prepended to a variable takes precedence over the `abs'
function call:

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

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...