Einzelnen Beitrag anzeigen

Daniel B
(Gast)

n/a Beiträge
 
#1

Dezimalzahlen in Römische Zahlen umwandeln und umgekehrt

  Alt 12. Jul 2003, 17:01
Hallo,

hiermit kann man normale Integer in römische Zahlen umwandeln und sich als String zurück geben lassen.
Delphi-Quellcode:
function DecToRoman(Decimal: Longint): String;
const
  Romans: Array[1..16] of String = ('I', 'IV', 'V', 'IX', 'X', 'XL',
                                    'L', 'XC', 'C', 'CD', 'D', 'CM',
                                    'M', '(M)', '[M]', '{M}');
  Arabics: Array[1..16] of Integer = (1, 4, 5, 9, 10, 40, 50, 90, 100,
                                      400, 500, 900, 1000, 10000, 100000, 1000000);
var
   iFor: Integer;
begin
  Result := '';
  for iFor := 16 downto 1 do
  begin
    while(Decimal >= Arabics[iFor]) do
    begin
      Decimal := Decimal - Arabics[iFor];
      Result := Result + Romans[iFor];
    end;
  end;
end;
So wäre der Aufruf:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(DecToRoman(1234567));
end;
Grüsse, Daniel
  Mit Zitat antworten Zitat