From: ideal one Date: 2011-10-15T04:22:26+09:00 Subject: Re: Ruby functions to split numbers Jeremy Bopp wrote in post #1026670: > On 10/14/2011 13:52, ideal one wrote: >> Var2 = 02305 # split remaining digits with preserving leading Zero if >> it exists >> >> i tried doing something like this, not sure if its correct >> >> sprintf("%2d", number) >> >> I am not able to preserve leading zero or i get fixnum to string error > > How do you want to work with your numbers within your program? Do you > want them to be real numbers or strings? As numbers, you would need to > tell sprintf how large the resulting string should be and with what to > pad the string in case the number is too short to fill the field. > > It's a bit long, but the documentation for sprintf should help you with > the details. Come back if you have a specific question about sprintf > itself: > > http://rdoc.info/stdlib/core/1.9.2/Kernel:sprintf > > Check out what this does: > > sprintf("%010d", 1234567) > > If you want to work with the numbers as strings in your program, you > don't need to use the sprintf method at all since they will already be > strings with the necessary padding, assuming Number (from your example) > is sufficiently padded and your splitting process avoids dropping > anything. > > -Jeremy Thanks, I do not need to be number to be string, i was only trying to preserve the leading Zero as ruby would treat it as octal. Let me try examples mentioned here, it should help me!! Cheers -- Posted via http://www.ruby-forum.com/.