Negative Numbers

Discussion in 'Ruby' started by Marston A., Jul 13, 2006.

  1. Marston A.

    Marston A. Guest

    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/.
     
    Marston A., Jul 13, 2006
    #1
    1. Advertising

  2. 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
     
    Justin Collins, Jul 13, 2006
    #2
    1. Advertising

  3. Marston A.

    gekkonier Guest

    gekkonier, Jul 13, 2006
    #3
  4. Marston A.

    Mat Schaffer Guest

    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
     
    Mat Schaffer, Jul 13, 2006
    #4
  5. Marston A.

    Craig Kim Guest

    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" <>
    To: "ruby-talk ML" <>
    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
    >
    >
    >
     
    Craig Kim, Jul 13, 2006
    #5
  6. Marston A.

    Scott Hill Guest

    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 <> 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" <>
    > To: "ruby-talk ML" <>
    > 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
     
    Scott Hill, Jul 13, 2006
    #6
  7. Marston A.

    Marston A. Guest

    Marston A., Jul 13, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Negative Numbers?

    Charles A. Lackman, Sep 28, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    2,600
    Jay B. Harlow [MVP - Outlook]
    Sep 28, 2004
  2. prem_eda
    Replies:
    5
    Views:
    7,875
    Pieter Hulshoff
    Oct 11, 2004
  3. Mayank
    Replies:
    2
    Views:
    984
    Gunnar Hjalmarsson
    Nov 28, 2003
  4. Replies:
    3
    Views:
    12,548
    Boris Stumm
    Feb 9, 2006
  5. Matthew Wilson

    negative numbers and integer division

    Matthew Wilson, Oct 3, 2003, in forum: Python
    Replies:
    7
    Views:
    378
    mensanator
    Oct 3, 2003
Loading...

Share This Page