Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Base of Numbers

Reply
Thread Tools

Base of Numbers

 
 
Volker Grabsch
Guest
Posts: n/a
 
      07-02-2003
Hello Folks!

I just wanted to know if there's any way except "printf"
to print out (or convert to string) a number using a
different base than 10.

There's "to_s()" for Integers taking a base, but is there
anything within the "#{i}"-syntax, within the contructors
or within the language?

... anything except printf that allows me to change the
base of a number?


Thanks,

--
Volker Grabsch
---<<(())>>---
\frac{\left|\vartheta_0\times\{\ell,\kappa\in\Re\} \right|}{\sqrt
[G]{-\Gamma(\alpha)\cdot\mathcal{B}^{\left[\oint\!c_\hbar\right]}}}
 
Reply With Quote
 
 
 
 
Volker Grabsch
Guest
Posts: n/a
 
      07-02-2003
Sorry, small self-correction:

In article <bdurb0$3o4$01$>, Volker Grabsch wrote:
> Hello Folks!
>
> I just wanted to know if there's any way except "printf"
> to print out (or convert to string) a number using a
> different base than 10.
>
> There's "to_s()" for Integers taking a base, but is there

<no>

> anything within the "#{i}"-syntax, within the contructors
> or within the language?
>
> .. anything except printf that allows me to change the
> base of a number?
>
>
> Thanks,


Well, this makes more sense ...

--
Volker Grabsch
---<<(())>>---
\frac{\left|\vartheta_0\times\{\ell,\kappa\in\Re\} \right|}{\sqrt
[G]{-\Gamma(\alpha)\cdot\mathcal{B}^{\left[\oint\!c_\hbar\right]}}}
 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      07-02-2003
>>>>> "V" == Volker Grabsch <> writes:

>> There's "to_s()" for Integers taking a base, but is there

V> <no>

In 1.8, #to_s can take an argument

svg% ruby -e 'p 12.to_s(16); p 12.to_s(12)'
"c"
"10"
svg%

>> .. anything except printf that allows me to change the
>> base of a number?


perhaps #sprintf ???


Guy Decoux



 
Reply With Quote
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      07-03-2003
Hi,

At Thu, 3 Jul 2003 07:25:30 +0900,
Josef 'Jupp' Schugt wrote:
> to_s works for any basis from 2 to 36 using these digits:
>
> 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, a, b, c, d, e, f, g, h,
> i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z


Also String#to_i.

$ grep -w 36 sample/test.rb
test_ok("Just".to_i(36) == 926381)
test_ok("-another".to_i(36) == -23200231779)
test_ok(1299022.to_s(36) == "ruby")
test_ok(-1045307475.to_s(36) == "-hacker")
test_ok("Just_another_Ruby_hacker".to_i(36) == 265419172580680477752431643787347)
test_ok(-265419172580680477752431643787347.to_s(36) == "-justanotherrubyhacker")

--
Nobu Nakada

 
Reply With Quote
 
Tom Felker
Guest
Posts: n/a
 
      07-03-2003
On Wed, 2003-07-02 at 17:25, Josef 'Jupp' Schugt wrote:
> Saluton!
>
> * Dave Thomas; 2003-07-02, 20:26 UTC:
> > This is a test 'ri'. Please report errors and omissions
> > on http://www.rubygarden.org/ruby?RIOnePointEight
> >
> > ------------------------------------------------------------ Fixnum#to_s
> > fix.to_s( base=10 ) -> aString
> >
> > ------------------------------------------------------------------------
> > Returns a string containing the representation of fix radix base
> > (2, 8, 10, or 16).
> > 12345.to_s #=> "12345"
> > 12345.to_s(2) #=> "11000000111001"
> > 12345.to_s( #=> "30071"
> > 12345.to_s(10) #=> "12345"
> > 12345.to_s(16) #=> "3039"

>
> to_s works for any basis from 2 to 36 using these digits:
>
> 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, a, b, c, d, e, f, g, h,
> i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
>
> Gis,
>
> Josef 'Jupp' Schugt



Cool. Is there a way to from a string to an int with any base?

--
Tom Felker <>


 
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
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Format of compiler generated derived destructor when base has 'virtual ~base() throw():" qazmlp C++ 1 04-10-2005 03:09 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57