Please explain that

Discussion in 'Ruby' started by Jan Pilz, Sep 10, 2008.

  1. Jan Pilz

    Jan Pilz Guest

    Can you please explain this behaviour ?

    irb(main):017:0> 071.to_s
    => "57"
    irb(main):018:0> 71.to_s
    => "71"

    Why is an octal Number converted to decimal when outputted as String ?
     
    Jan Pilz, Sep 10, 2008
    #1
    1. Advertising

  2. Jan Pilz

    Xavier Noria Guest

    On Wed, Sep 10, 2008 at 3:24 PM, Jan Pilz <> wrote:

    > Can you please explain this behaviour ?
    >
    > irb(main):017:0> 071.to_s


    On the one hand a leading "0" in an integer literal like that tells
    Ruby you are using base 8. On the other hand #to_s prints in base 10
    (by default). You've got the same number, only written in different
    ways.
     
    Xavier Noria, Sep 10, 2008
    #2
    1. Advertising

  3. > Can you please explain this behaviour ?
    >
    > irb(main):017:0> 071.to_s
    > => "57"
    > irb(main):018:0> 71.to_s
    > => "71"
    >
    > Why is an octal Number converted to decimal when outputted as String ?


    Let's take a look at ri Fixnum#to_s:

    fix.to_s( base=10 ) -> aString
    ------------------------------------------------------------------------
    Returns a string containing the representation of _fix_ radix
    _base_ (between 2 and 36).

    Here you have it - default base is 10.

    Regards,
    Rimantas
    --
    http://rimantas.com/
     
    Rimantas Liubertas, Sep 10, 2008
    #3
  4. On Wed, Sep 10, 2008 at 9:24 AM, Jan Pilz <> wrote:
    > Can you please explain this behaviour ?
    >
    > irb(main):017:0> 071.to_s
    > => "57"
    > irb(main):018:0> 71.to_s
    > => "71"
    >
    > Why is an octal Number converted to decimal when outputted as String ?


    Leave off the to_s:

    >> 071

    => 57

    You can represent your numbers in octal by putting a leading 0, but
    they will be accessed as decimal numbers throughout your application.
    If you want to display (any) number as octal, just use:

    >> 071.to_s(8)

    => "71"
    >> 57.to_s(8)

    => "71"


    --
    Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
    stuff at: http://metametta.blogspot.com
     
    Gregory Brown, Sep 10, 2008
    #4
  5. Jan Pilz

    Jano Svitok Guest

    On Wed, Sep 10, 2008 at 15:24, Jan Pilz <> wrote:
    > Can you please explain this behaviour ?
    >
    > irb(main):017:0> 071.to_s
    > => "57"
    > irb(main):018:0> 71.to_s
    > => "71"
    >
    > Why is an octal Number converted to decimal when outputted as String ?


    It's not an octal number anymore when it's parsed. It's just a number.
     
    Jano Svitok, Sep 10, 2008
    #5
  6. Jan Pilz

    Xavier Noria Guest

    On Wed, Sep 10, 2008 at 3:33 PM, Gregory Brown
    <> wrote:

    > You can represent your numbers in octal by putting a leading 0, but
    > they will be accessed as decimal numbers throughout your application.


    In fact the application itself has no sense of bases, applications
    deal with numbers. Bases enter the game in literals, printing, etc.
     
    Xavier Noria, Sep 10, 2008
    #6
  7. On Wed, Sep 10, 2008 at 9:47 AM, Xavier Noria <> wrote:
    > On Wed, Sep 10, 2008 at 3:33 PM, Gregory Brown
    > <> wrote:
    >
    >> You can represent your numbers in octal by putting a leading 0, but
    >> they will be accessed as decimal numbers throughout your application.

    >
    > In fact the application itself has no sense of bases, applications
    > deal with numbers. Bases enter the game in literals, printing, etc.


    Right, I was mainly referring to the defaults for interacting with
    numbers, but I could have been more clear.

    -greg

    --
    Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
    stuff at: http://metametta.blogspot.com
     
    Gregory Brown, Sep 10, 2008
    #7
  8. On Wed, Sep 10, 2008 at 10:24:18PM +0900, Jan Pilz wrote:
    > Can you please explain this behaviour ?
    >
    > irb(main):017:0> 071.to_s
    > => "57"
    > irb(main):018:0> 71.to_s
    > => "71"
    >
    > Why is an octal Number converted to decimal when outputted as String ?


    Check the docs for Fixnum#to_s. It takes a base argument, which defaults to
    10. Also, I'll point out that a number isn't octal or decimal or anything
    else. It can be represented in whatever base, but it's just a number.

    --Greg
     
    Gregory Seidman, Sep 10, 2008
    #8
    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. suzy
    Replies:
    3
    Views:
    450
  2. BluDog

    Can Someone Please Explain...

    BluDog, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    477
    BluDog
    Oct 5, 2004
  3. Alan Silver
    Replies:
    1
    Views:
    483
    Alan Silver
    Jun 28, 2005
  4. KK
    Replies:
    2
    Views:
    595
    Big Brian
    Oct 14, 2003
  5. Kaye Ng
    Replies:
    8
    Views:
    178
    Josh Cheek
    Jun 8, 2010
Loading...

Share This Page