ruby - How to work with leading zeros in integers -


what proper way deal leading zeros in ruby?

0112.to_s  => "74" 0112.to_i  => 74 

why converting 0112 74?

how can convert 0112 string "0112" ?


i want define method takes integer argument , returns digits in descending order.

but not seem work me when have leading zeros:

def descending_order(n)   n.to_s.reverse.to_i end 

a numeric literal starts 0 octal representation, except literals start 0x represent hexadecimal numbers or 0b represent binary numbers.

1 * 8**2 + 1 * 8**1 + 2 * 8**0 == 74 

to convert 0112, use string#% or kernel#sprintf appropriate format string:

'0%o' % 0112  # 0: leading zero, %o: represent octal # => "0112" 

Comments

Popular posts from this blog

Add new key value to json node in java -

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

javascript - Highcharts Synchronized charts with missing data points -