Base of Numbers

V

Volker Grabsch

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,
 
V

Volker Grabsch

Sorry, small self-correction:

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,

Well, this makes more sense ...
 
T

ts

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%

perhaps #sprintf ???


Guy Decoux
 
N

Nobuyoshi Nakada

Hi,

At Thu, 3 Jul 2003 07:25:30 +0900,
Josef said:
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")
 
T

Tom Felker

Saluton!

* Dave Thomas; 2003-07-02, 20:26 UTC:

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top