Ruby functions to split numbers

HI All,
I have couple of requirements to be handled in ruby, it will be
great if anyone can shed some light on this.

  1. i have a 8 digit number, to be used in my code as 2 parts(split),
    like shown below.

Number = “34502305” # I put them in double quotes, so that leading
zeros are preserved.
Var1 = 345 # split firs 3 digits always
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

Cheers

ruby-1.9.2-p290 :010 > a = “01234567”
=> “01234567”
ruby-1.9.2-p290 :011 > var1,var2 = [a[0,3],a[3,5]]
=> [“012”, “34567”]
ruby-1.9.2-p290 :012 > var1
=> “012”
ruby-1.9.2-p290 :013 > var2
=> “34567”

keep in mind that a number with a leading zero makes ruby think the base
is
octal:

ruby-1.9.2-p290 :019 > 012
=> 10

hope that helps.

ian

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

Jeremy B. 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

On Fri, Oct 14, 2011 at 4:54 PM, ideal one [email protected]
wrote:

keep in mind that a number with a leading zero makes ruby think the base
Hello there,

Posted via http://www.ruby-forum.com/.

Strings have implemented on them the method “next” which, for numerical
strings, seems to respect numerical iteration

n = ‘0’
15.times do
n = n.next # => “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”,
“11”,
“12”, “13”, “14”, “15”
end

I’m not 100% sure that this is compatible across all versions of Ruby,
you
should definately have a test to make sure that it increments them as
you
expect. You should also consider what happens when you have something
like
“999”, is it supposed to go to “1000”, which would cause it to gain a
digit?
If not, you’ll need to add special handling for that situation.

On Fri, Oct 14, 2011 at 11:54 PM, ideal one [email protected]
wrote:

keep in mind that a number with a leading zero makes ruby think the base
is
octal:

ruby-1.9.2-p290 :019 > 012
=> 10

hope that helps.

  How can i auto increment the number, i tried doing

something like this…
var1 is always constant, but i want to autoincrement var2

value = var2 + 1 # iam getting Cannot convert Fixnum to String Error.
is something like this works var2++, not tried this in ruby.
Preserve leading Zero and also autoincrement…

I am still not sure I get what you really want to do with those
numbers. What exactly is your input (String or a Fixnum) and what do
you want to do with it?

Here is one more way to split and assign to local variables:

irb(main):009:0> s = “1234567”
=> “1234567”
irb(main):010:0> /\A(?\d{1,3})(?\d{5})\z/ =~ s
=> 0
irb(main):011:0> v1
=> “12”
irb(main):012:0> v2
=> “34567”

But if your input is already numeric other approaches are usually
better, e.g.

irb(main):013:0> n = 1234567
=> 1234567
irb(main):014:0> v1 = n / 100000
=> 12
irb(main):015:0> v2 = n % 100000
=> 34567

(Assuming we’re talking about decimal representation.)

Kind regards

robert

Ian M. Asaff wrote in post #1026669:

ruby-1.9.2-p290 :010 > a = “01234567”
=> “01234567”
ruby-1.9.2-p290 :011 > var1,var2 = [a[0,3],a[3,5]]
=> [“012”, “34567”]
ruby-1.9.2-p290 :012 > var1
=> “012”
ruby-1.9.2-p290 :013 > var2
=> “34567”

keep in mind that a number with a leading zero makes ruby think the base
is
octal:

ruby-1.9.2-p290 :019 > 012
=> 10

hope that helps.

ian

Hello there,
How can i auto increment the number, i tried doing
something like this…
var1 is always constant, but i want to autoincrement var2

value = var2 + 1 # iam getting Cannot convert Fixnum to String Error.
is something like this works var2++, not tried this in ruby.
Preserve leading Zero and also autoincrement…

Cheers

Robert K. wrote in post #1026992:

On Fri, Oct 14, 2011 at 11:54 PM, ideal one [email protected]
wrote:

keep in mind that a number with a leading zero makes ruby think the base
is
octal:

ruby-1.9.2-p290 :019 > 012
=> 10

hope that helps.

  How can i auto increment the number, i tried doing

something like this…
var1 is always constant, but i want to autoincrement var2

value = var2 + 1 # iam getting Cannot convert Fixnum to String Error.
is something like this works var2++, not tried this in ruby.
Preserve leading Zero and also autoincrement…

I am still not sure I get what you really want to do with those
numbers. What exactly is your input (String or a Fixnum) and what do
you want to do with it?

Here is one more way to split and assign to local variables:

irb(main):009:0> s = “1234567”
=> “1234567”
irb(main):010:0> /\A(?\d{1,3})(?\d{5})\z/ =~ s
=> 0
irb(main):011:0> v1
=> “12”
irb(main):012:0> v2
=> “34567”

But if your input is already numeric other approaches are usually
better, e.g.

irb(main):013:0> n = 1234567
=> 1234567
irb(main):014:0> v1 = n / 100000
=> 12
irb(main):015:0> v2 = n % 100000
=> 34567

(Assuming we’re talking about decimal representation.)

Kind regards

robert

Hi All,

       Sorry if I am not clear with my requirement, Let me try to

explain in detail…
I have to create few Accounts, so the account number i am providing to
the system manually each time and it should be in series.
I have to enter the 7 digit account numbers in two text box in my
application.
the First 3 digits goes in first text box and remaining 4 digits goes in
the second text box, that is why i was looking for a Split function.

eg: Default or starting Account No - 1230567, so next one in series
should be 1230568,1230569…so on. I need to increment this account
number everytime, so that my code picks the next correct number.
Secondly, i have to place these account numbers in two different text
boxes.
first 3 digits, ie 123 goes into first text box
remaining 4 digits goes into second text box.

Increment and Splitting the numbers in my code, I am fine whether the
number is treated as string or number, if we are able to handle the
leading zeros…
Let me know if you require more information.

Cheers

Josh C. wrote in post #1026729:

On Fri, Oct 14, 2011 at 4:54 PM, ideal one [email protected]
wrote:

keep in mind that a number with a leading zero makes ruby think the base
Hello there,

Posted via http://www.ruby-forum.com/.

Strings have implemented on them the method “next” which, for numerical
strings, seems to respect numerical iteration

n = ‘0’
15.times do
n = n.next # => “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”,
“11”,
“12”, “13”, “14”, “15”
end

I’m not 100% sure that this is compatible across all versions of Ruby,
you
should definately have a test to make sure that it increments them as
you
expect. You should also consider what happens when you have something
like
“999”, is it supposed to go to “1000”, which would cause it to gain a
digit?
If not, you’ll need to add special handling for that situation.

hi there…
n.next works perfectly for me, I am using ruby 1.8.7 version.
i tried 999, 1239999 and also other variations i can think of, it works
perfectly.

Cheers

ideal one wrote in post #1026668:

HI All,
I have couple of requirements to be handled in ruby, it will be
great if anyone can shed some light on this.

  1. i have a 8 digit number, to be used in my code as 2 parts(split),
    like shown below.

Number = “34502305” # I put them in double quotes, so that leading
zeros are preserved.
Var1 = 345 # split firs 3 digits always
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

Cheers

if your numbers are Fixnum, maybe you can do

n = 34502305
n2 = n % 100_000
n1 = (n - n2) / 100_000
s1 = n1.to_s.rjust(3,“0”)
s2 = n2.to_s.rjust(5,“0”)

of course it does not do the trick in some situations, eg when n <
100_000

On Mon, Oct 17, 2011 at 7:48 PM, ideal one [email protected]
wrote:

hope that helps.
numbers. What exactly is your input (String or a Fixnum) and what do
irb(main):012:0> v2
=> 34567
explain in detail…
Secondly, i have to place these account numbers in two different text
boxes.
first 3 digits, ie 123 goes into first text box
remaining 4 digits goes into second text box.

Increment and Splitting the numbers in my code, the better way i was
looking for…

def split_account(no)
high, low = no.to_int.divmod 10_000
["%03d" % high, “%04d” % low]
end

100.times do |num|
a, b = split_account num

end

or

123456.upto 123470 do |num|
a, b = split_account num

end

Instead of “” % x you can also use sprintf - it’s the same.

Let me know if you require more information.

Where do you need the leading zeros?

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs