简体   繁体   English

字符串中的递增数字

[英]Increment number from a string

So one of the projects we have for one of our clients allows them to print checks. 因此,我们为一位客户提供的一个项目允许他们打印支票。 One of the fields that goes on the check is check number. 支票上的字段之一是支票号。 Currently we are storing the check numbers(for reporting purposes) in a database as an int type. 当前,我们将支票号码(出于报告目的)以int类型存储在数据库中。

The problem is that the client wants to increase the size of the check number to 20 digits. 问题是客户希望将支票号码的大小增加到20位。 One of the features of the project is to auto assign check numbers to each check when printing multiple checks. 该项目的功能之一是在打印多张支票时自动为每张支票分配支票号码。

So for example they are printing 3 checks. 例如,他们正在打印3张支票。 They enter the starting check number as "100123" and then click an "Assign Check Numbers" button at witch point the first check to be printed gets the "100123" that was entered and each following check is given the same number incremented by one so the second check would get "100124", the third would get "100125" and so on. 他们输入起始支票号码为“ 100123”,然后在女巫点处单击“分配支票号码”按钮。要打印的第一张支票得到输入的“ 100123”,随后的每张支票都被赋予相同的号码,加一第二个检查将得到“ 100124”,第三个将得到“ 100125”,依此类推。

The problem is that they want a 20 digit check number which is too big for any numerical data type. 问题在于他们想要20位校验码,对于任何数字数据类型而言,校验码都太大。 My only solution is to change the check number to string/varchar but that would break the "Assign Check Numbers" process for multiple checks since I can't increment the string to get the next number. 我唯一的解决方案是将支票号更改为字符串/ varchar,但这会中断多次支票的“分配支票号”过程,因为我无法递增字符串以获取下一个号码。

Any ideas on how I could implement such a requirement? 关于如何实施此类要求的任何想法?

请使用BigIntegerdecimal ,因为两者的精度都超出了要求。

Actually the range of a ulong goes from 0 to 18,446,744,073,709,551,615 , which is 20 digits. 实际上, ulong的范围从018,446,744,073,709,551,615 ,即20位数字。 I'm assuming there won't be any "negative" check numbers. 我假设不会有任何“负”支票号码。 So long as the first two numbers of the check are less than 17, you can do this: 只要支票的前两个数字小于17,您就可以这样做:

ulong checkNumber = ulong.Parse(textCheckNumber);
checkNumber++;
string newCheckNumber = checkNumber.ToString();

If there is a possibility that the first two digits are greater than or equal to 17, then you can do this: 如果前两位数字有可能大于或等于17,则可以执行以下操作:

ulong checkNumber = ulong.Parse(textCheckNumber.SubString(2));
checkNumber++;
string newCheckNumber = textCheckNumber.SubString(0,2) + checkNumber;

You could use long . 您可以使用long Its max value is 2(^64)-1 . 其最大值是2(^64)-1

Thanks for all the feedback everyone. 感谢大家的反馈。

Saving to the database as varchar should be fine. 保存为varchar到数据库应该没问题。 Here is what I came up with for the increment based on everyone's feedback. 这是我根据每个人的反馈想出的增量。

string sampleNum = "01999999999999999997";

for (int i = 0; i < 5; i++)
{
    int first = int.Parse(sampleNum.Substring(0, 2));
    ulong rest = ulong.Parse(sampleNum.Substring(2));

    rest++;
    if (rest > 999999999999999999)
    {
        first++;
        rest = 0;
    }

    sampleNum = first.ToString().PadLeft(2, '0') + rest.ToString().PadLeft(18, '0');

    Console.WriteLine(sampleNum);
}

Here is what I got in the test: 这是我在测试中得到的:

01999999999999999998 
01999999999999999999 
02000000000000000000 
02000000000000000001 
02000000000000000002 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM