Mirror

How to convert an integer value to a Roman Numeral representation (Views: 101)


Problem/Question/Abstract:

How to convert an integer value to a Roman Numeral representation

Answer:

Converts an integer value to a string containing a roman numeric code ("XVII"):

{Parameters:   - Num: Integer to convert.
Return Value:  - Roman numerical representation of the passed integer value.
History:  12/7/99 "Philippe Ranger" (PhilippeRanger@compuserve.com)}

function IntToRoman(num: Cardinal): string; {returns num in capital roman digits}
const
  Nvals = 13;
  vals: array[1..Nvals] of word = (1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
    1000);
  roms: array[1..Nvals] of string[2] = ('I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC',
    'C', 'CD', 'D', 'CM', 'M');
var
  b: 1..Nvals;
begin
  result := '';
  b := Nvals;
  while num > 0 do
  begin
    while vals[b] > num do
      dec(b);
    dec(num, vals[b]);
    result := result + roms[b]
  end;
end;

<< Back to main page