Skip to content

Instantly share code, notes, and snippets.

@jspeis
Created January 10, 2012 03:55
Show Gist options
  • Save jspeis/1586821 to your computer and use it in GitHub Desktop.
Save jspeis/1586821 to your computer and use it in GitHub Desktop.
convert arabic numeral to roman numerals
sub to_roman_numeral {
my %roman = (
1_000_000 => '(MBAR)',
900_000 => '(CBAR)(MBAR)',
500_000 => '(DBAR)',
400_000 => '(CBAR)(DBAR)',
100_000 => '(CBAR)',
90_000 => '(XBAR)(CBAR)',
50_000 => '(LBAR)',
40_000 => '(XBAR)(LBAR)',
10_000 => '(XBAR)',
9_000 => 'M(XBAR)',
5_000 => '(VBAR)',
1_000 => 'M',
900 => 'CM',
500 => 'D',
400 => 'CD',
100 => 'C',
90 => 'XC',
50 => 'L',
40 => 'XL',
10 => 'X',
9 => 'IX',
5 => 'V',
4 => 'IV',
1 => 'I'
);
my $num = shift;
my $outputStr = "";
foreach my $key (sort {$b <=> $a} keys %roman) {
while ($num - $key >= 0) {
$outputStr = $outputStr . $roman{$key};
$num -= $key;
}
}
return $outputStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment