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
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 Fri, Oct 14, 2011 at 2:52 PM, ideal one <idealone5@hotmail.com> wrote:
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
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:
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
···
On 10/14/2011 13:52, ideal one wrote:
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
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
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:
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!!
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..
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 4:54 PM, ideal one <idealone5@hotmail.com> wrote:
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..
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(?<v1>\d{1,3})(?<v2>\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
···
On Fri, Oct 14, 2011 at 11:54 PM, ideal one <idealone5@hotmail.com> 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..
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(?<v1>\d{1,3})(?<v2>\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, the better way i was
looking for....
Let me know if you require more information.
Cheers
···
On Fri, Oct 14, 2011 at 11:54 PM, ideal one <idealone5@hotmail.com> > wrote:
On Fri, Oct 14, 2011 at 4:54 PM, ideal one <idealone5@hotmail.com> > 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.
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(?<v1>\d{1,3})(?<v2>\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, the better way i was
looking for....