PoshCode Logo PowerShell Code Repository

get-roman by Luis C 4 weeks ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6818"></script>download | new post

Short powershell module to convert numbers to Roman numerals

  1. function get-roman ([int]$myNum)
  2. {
  3.     if ($myNum -ge 4000 -or $myNum -le 0)
  4.     {
  5.         "$myNum is not a good one"
  6.     } else {
  7.         $myRomans = [Ordered]@{ M=1000;CM=900;D=500;CD=400;C=100;XC=90;L=50;XL=40;X=10;IX=9;V=5;IV=4;I=1 }
  8.         foreach ($key in $myRomans.Keys)
  9.         {
  10.             while ($myNum -ge  $myRomans.item($key))
  11.             {
  12.                  $myOut += $key; # build Roman numeral
  13.                  $myNum -= $myRomans.item($key) # subtract value from given number
  14.             }
  15.         }
  16.         $myOut
  17.     }
  18. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me