[英]How to apply incrementing numbers from strings (invoice numbers)
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
). 当前,发票号如下所示:
Q20140001
,并且应该为每个新发票将其增加1(因此下一个发票号将为Q20140002
)。 All I would like to do is split the string ( in to Q
and 20140001
). 我只想将字符串拆分(分别为
Q
和20140001
)。 Then I would like to have the 20140001
number increment to 20140002
. 然后我想将
20140001
数字增加到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. 我的猜测是,每当我要创建一个新编号时,我都会检查数据库中存储的最后一个发票编号,将其解析为字符串和整数,取整数并将其递增1,然后加上Q回到它。
I'm not sure if my approach is correct, though. 不过,我不确定我的方法是否正确。
Did you tried using the String#succ
method? 您是否尝试过使用
String#succ
方法?
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: 您必须记住的一件事是,在99999999发票之后,结果可能不是您想要的:
2.1.2 :002 > 'Q99999999'.succ
=> "R00000000"
2.1.2 :003 >
Q
will be incremented to the next ASCII position R
, resulting in R00000000
. Q
将增加到下一个ASCII位置R
,结果为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. 一种可能的解决方案是用足够的零填充发票编号,例如:
Q00000000020140001
或保留原样,从长远来看可能会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.