Thema: Delphi Neuer Zahlentyp - Int128

Einzelnen Beitrag anzeigen

jbg

Registriert seit: 12. Jun 2002
3.483 Beiträge
 
Delphi 10.1 Berlin Professional
 
#3
  Alt 11. Mai 2003, 11:05
Zitat von tommie-lie:
IntToStren
Das ist gar nicht nötig.
1. Ist es relativ langsam
2. Wenn man schon Werte von 0..9 hat, kann man gleich Ord('0') dazurechnen und hat die Ziffer.

Delphi-Quellcode:
type
  TDezZahl = record
    Negativ: Boolean;
    Stellen: TByteDynArray;
  end;

procedure DezZahlConvertError(const Zahl: string);
begin
  raise EConvertError.CreateFmt('"%s" ist keine gültige Zahl', [Zahl]);
end;

function DezZahlToStr(const Zahl: TDezZahl): string;
var
  i: Integer;
  StartIndex: Integer;
begin
  if Zahl.Negativ then StartIndex := 2 else StartIndex := 1;
  SetLength(Result, Length(Zahl.Stellen) + StartIndex - 1);

  if Zahl.Negativ then
    Result[1] := '-';

  for i := 0 to High(Zahl.Stellen) do
    Result[i + StartIndex] := Char(Ord('0') + Zahl.Stellen[i]);
end;

function StrToDezZahl(const Zahl: string): TDezZahl;
var
  i: Integer;
  StartIndex: Integer;
begin
  if (Length(Zahl) = 0) or (not (Zahl[1] in ['0'..'9', '-', '+'])) then
    DezZahlConvertError(Zahl);

  Result.Negativ := (Zahl[1] = '-');
  if (Result.Negativ) or (Zahl[1] = '+') then
    StartIndex := 2
  else
    StartIndex := 1;

  SetLength(Result.Stellen, Length(Zahl) - (StartIndex - 1));

  for i := StartIndex to Length(Zahl) - 1 do
  begin
    if not (Zahl[i] in ['0'..'9']) then
      DezZahlConvertError(Zahl);

    Result.Stellen[i - StartIndex] := Byte(Ord(Zahl[i]) - Ord('0'));
  end;
end;
  Mit Zitat antworten Zitat