Conv-Roman Numerals

From Vectorlab
Jump to: navigation, search


Converts a number into roman numerals: for example: "308" becomes "CCCVIII". By Petri Sakkinen

{ by Petri Sakkinen 2009 }
{ Based on a published algorithm from }
{ http://computeralgorithms.blogspot.com/2008/02/convert-decimal-values-to-roman.html }
{ "We won't bother with the larger values in this routine  }
{ and limit our numbers to values between 1 and 3999." }
FUNCTION RomanValue(inputValue : INTEGER): STRING; 
VAR
	tempS : STRING; 

BEGIN 
	IF ((inputValue > 3999) | (inputValue < 1)) THEN tempS := 'N/A' ELSE BEGIN 

		WHILE (inputValue > 999) DO BEGIN 
			tempS := CONCAT(tempS, 'M'); 
			inputValue := inputValue - 1000; 
		END; 

		IF (inputValue > 899) THEN BEGIN 
			tempS := CONCAT(tempS, 'CM'); 
			inputValue := inputValue - 900; 
		END; 

		IF (inputValue > 499) THEN BEGIN 
			tempS := CONCAT(tempS, 'D'); 
			inputValue := inputValue - 500; 
		END; 

		IF (inputValue > 399) THEN BEGIN 
			tempS := CONCAT(tempS, 'CD'); 
			inputValue := inputValue - 400; 
		END; 

		WHILE (inputValue > 99) DO BEGIN 
			tempS := CONCAT(tempS, 'C'); 
			inputValue := inputValue - 100; 
		END; 

		IF (inputValue > 89) THEN BEGIN 
			tempS := CONCAT(tempS, 'XC'); 
			inputValue := inputValue - 90; 
		END; 

		IF (inputValue > 49) THEN BEGIN 
			tempS := CONCAT(tempS, 'L'); 
			inputValue := inputValue - 50; 
		END; 

		IF (inputValue > 39) THEN BEGIN 
			tempS := CONCAT(tempS, 'XL'); 
			inputValue := inputValue - 40; 
		END; 

		WHILE (inputValue > 9) DO BEGIN 
			tempS := CONCAT(tempS, 'X'); 
			inputValue := inputValue - 10; 
		END; 

		IF (inputValue > 8) THEN BEGIN 
			tempS := CONCAT(tempS, 'IX'); 
			inputValue := inputValue - 9; 
		END; 

		IF (inputValue > 4) THEN BEGIN 
			tempS := CONCAT(tempS, 'V'); 
			inputValue := inputValue - 5; 
		END; 

		IF (inputValue > 3) THEN BEGIN 
			tempS := CONCAT(tempS, 'IV'); 
			inputValue := inputValue - 4; 
		END; 

		WHILE (inputValue > 0) DO BEGIN 
			tempS := CONCAT(tempS, 'I'); 
			inputValue := inputValue - 1; 
		END; 
		
		RomanValue := tempS; 
	END; 
END;