# Conversion Integer to Roman Numeral

i want to convert integer to roman numeral. As we know, format for roman numeral is like this:

• 1 = I
• 5 = V
• 10 = X
• 50 = L
• 100 = C
• 500 = D
• 1000 = M

For example, if we have number 497, it will be CDXCVII.

In Java, i know we can use Switch Case for the condition. Is there any idea to implement this in apex?

• as switch-case is not supported in Apex, replace it with if-else condition for java algorithm Commented Nov 29, 2017 at 9:53
• Hi @AlexanderBerehovskiy, i see.. when i try to use code from comment section, it will be good if using While. Btw thanks for your comment.
– PoH
Commented Nov 29, 2017 at 10:37

I don't think there is any native functionality to implement this. So you need to write custom logic to implement this. As Java and Apex share similar code concept so you can take help from there

public static String IntegerToRomanNumeral(int input) {
if (input < 1 || input > 3999)
return "Invalid Roman Number Value";
String s = "";
while (input >= 1000) {
s += "M";
input -= 1000;        }
while (input >= 900) {
s += "CM";
input -= 900;
}
while (input >= 500) {
s += "D";
input -= 500;
}
while (input >= 400) {
s += "CD";
input -= 400;
}
while (input >= 100) {
s += "C";
input -= 100;
}
while (input >= 90) {
s += "XC";
input -= 90;
}
while (input >= 50) {
s += "L";
input -= 50;
}
while (input >= 40) {
s += "XL";
input -= 40;
}
while (input >= 10) {
s += "X";
input -= 10;
}
while (input >= 9) {
s += "IX";
input -= 9;
}
while (input >= 5) {
s += "V";
input -= 5;
}
while (input >= 4) {
s += "IV";
input -= 4;
}
while (input >= 1) {
s += "I";
input -= 1;
}
return s;
}


Converting Integers to Roman Numerals - Java

• Thanks a lot bro!! I try using this code and modify some part and BOOM its working :D.
– PoH
Commented Nov 29, 2017 at 10:37

Represent the number with just I, and then we replace the characters in ascending value of the roman characters.

String intToRoam(integer Num){
return  'I'.repeat(Num)
.replace('IIIII', 'V')
.replace('IIII', 'IV')
.replace('VV', 'X')
.replace('VIV', 'IX')
.replace('XXXXX', 'L')
.replace('XXXX', 'XL')
.replace('LL', 'C')
.replace('LXL', 'XC')
.replace('CCCCC', 'D')
.replace('CCCC', 'CD')
.replace('DD', 'M')
.replace('DCD', 'CM');
}

system.debug('Romanstr ' + intToRoam(497));

• Thanks for answer, interesting, i will try to use this concept.
– PoH
Commented Nov 29, 2017 at 10:39
• @Enriko You are Welcome.... Commented Nov 29, 2017 at 11:10