Главная страница | назад





Article #27799: How to convert numbers into roman numerals.

QUESTION:

How can I convert decimals into Roman numerals?

ANSWER:

Drop a button, two edit boxes and two labels on the form. Add the code below. Enter numbers into the edit boxes, then press the button, to see the conversion in the labels' captions.

function DecToRom(Dec: LongInt): String;
const
Nums : Array[1..13] of Integer =
(1, 4, 5, 9, 10, 40, 50, 90, 100,
400, 500, 900, 1000);
RomanNums: Array[1..13] of string =
('I', 'IV', 'V', 'IX', 'X', 'XL',
'L', 'XC', 'C', 'CD', 'D', 'CM', 'M');
var
i: Integer;
begin
Result := '';
for i := 13 downto 1 do
while (Dec>= Nums[i]) do
begin
Dec := Dec — Nums[i];
Result := Result + RomanNums[i];
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := DecToRom(StrToInt(Edit1.Text));
Label2.Caption := DecToRom(StrToint(Edit2.Text));
end;

Last Modified: 09-OCT-01