Jump to content

Recommended Posts

Good Day,
MyCode in Database = VARCHAR(20)

 

MyCode could be 

(120 or 250)  like 3 digit Integer,  or

(200.001  or  600.010)  there is a dot between numbers,  or

(500.005.0010 or 100.010.1500)   there are 2 dots between numbers

 

I need to Increase MyCode by 1  that is

if MyCode = 100 then requested result = 101

if MyCode = 300.001 then requested result = 300.002

if MyCode = 600.005.0010 then requested result = 600.005.0011

 

How can i do that ?
Thank You
 

 

 

 

 

Share this post


Link to post

I would suggest to look up TryStrToInt. But that is just the beginning. What happens if you add 1 to 999.9999? Are there rules for those grouping dots? If the dots are always at the same place you could remove them before conversion and put them back in after your math and reconversion to string.

Share this post


Link to post
34 minutes ago, Henry Olive said:

600.005.0011

  1. split the string based on the delimiter
  2. for the last string in the list, ('0011')
  3. convert to integer (11) and increment (12)
  4. convert the int back to a string ('12')
  5. compare the lengths and then loop to add in the padding zeros ('0012')
  6. concate the list back to 1 string
Edited by SwiftExpat
note to concat

Share this post


Link to post
2 hours ago, Henry Olive said:

Good Day,
MyCode in Database = VARCHAR(20)

 

MyCode could be 

(120 or 250)  like 3 digit Integer,  or

(200.001  or  600.010)  there is a dot between numbers,  or

(500.005.0010 or 100.010.1500)   there are 2 dots between numbers

 

I need to Increase MyCode by 1  that is

if MyCode = 100 then requested result = 101

if MyCode = 300.001 then requested result = 300.002

if MyCode = 600.005.0010 then requested result = 600.005.0011

 

How can i do that

 

Do it on the string itself, like you learned addition in school. :classic_cool:

function incrementID(const aID: string):string;
var
  ch: Char;
  N: Integer;
begin
  Result := aID;
  N:= Length(Result);
  while N > 0 do begin
    ch := Result[N];
    case ch of
      '0'..'8': begin
                  Result[N] := Succ(ch);
                  Break;
                end;
      '9': begin
             Result[N] := '0';
             Dec(N);
           end;
      '.': Dec(N);
    else
      raise Exception.CreateFmt('Invalid character "%s" in ID "%s" at position %d.',
        [ch, aID, N]);
    end; {case ch}
  end;  {while}
end;

 

  • Like 2

Share this post


Link to post

Hi, yes you can Split

 

procedure TForm23.Button1Click(Sender: TObject);
var S : TArray<String>;
begin
 S:=Edit3.Text.Split(['.']);
 S[length(S)-1]:=Format('%.3d',[(StrToInt(S[Length(S)-1])+1)]);
 Edit3.Text:=String.Join('.',S);
end;

 

Edited by Serge_G

Share this post


Link to post

@Fr0sT.Brutal, code was edited during your post.

Just a thing, this    MyCode = 600.005.0010 then requested result = 600.005.0011  should not work you have to manage a way to have the good formatstring

something like

procedure TForm23.Button1Click(Sender: TObject);
var S : TArray<String>;
    fmt : string;
begin
 S:=Edit3.Text.Split(['.']);
 fmt:='%.'+Length(S[Length(S)-1]).tostring+'d';
 S[length(S)-1]:=Format(fmt,[(StrToInt(S[Length(S)-1])+1)]);
 Edit3.Text:=String.Join('.',S);
end;

 

Edited by Serge_G
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×