I am trying to implement incrementing invoice numbers on my current application, however I am confused as to how to go about it as the applications invoices are a string.
Currently the invoice numbers look like this: Q20140001
and they are supposed to increase by 1 for each new invoice (so the next invoice number would be Q20140002
). All I would like to do is split the string ( in to Q
and 20140001
). Then I would like to have the 20140001
number increment to 20140002
.
My guess is that every time I want to create a new number, I check the last invoice number that was stored on the database, parse it into a string and an integer, take the integer and increment it by 1, and then add the Q back on to it.
I'm not sure if my approach is correct, though.
Did you tried using the String#succ
method?
IRB session:
2.1.2 :001 > 'Q20140001'.succ
=> "Q20140002"
2.1.2 :002 >
One thing you must keep in mind is, that after 99999999 invoices, the result may not be what you want:
2.1.2 :002 > 'Q99999999'.succ
=> "R00000000"
2.1.2 :003 >
Q
will be incremented to the next ASCII position R
, resulting in R00000000
. One possible solution is to fill the invoice number with enough zeros, something like: Q00000000020140001
or leave the things as they are, which may be better in the long run.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.