Functions/ConvertTo-RomanNumeral.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
function ConvertTo-RomanNumeral { <# .SYNOPSIS Converts a number to a Roman numeral. .DESCRIPTION Converts a number - in the range of 1 to 3,999 - to a Roman numeral. .PARAMETER Number An integer in the range 1 to 3,999. .PARAMETER IncludeInput Switch to include input values in the output .INPUTS System.Int32[] .OUTPUTS System.String .EXAMPLE ConvertTo-RomanNumeral -Number 1999 MCMXCIX .EXAMPLE 2002 | ConvertTo-RomanNumeral MMII .EXAMPLE ConvertTo-RomanNumeral 1918,1945 -IncludeInput Number RomanNumeral ------ ------------ 1918 MCMXVIII 1945 MCMXLV #> [CmdletBinding()] [OutputType([string])] Param ( [Parameter(Mandatory, HelpMessage = 'Enter an integer in the range 1 to 3,999', ValueFromPipeline, Position = 0)] [ValidateRange(1, 3999)] [int[]] $Number, [switch] $IncludeInput ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" $DecimalToRoman = @{ Thousands = '', 'M', 'MM', 'MMM' Hundreds = '', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM' Tens = '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC' Ones = '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX' } $column = @{ Thousands = 0 Hundreds = 1 Tens = 2 Ones = 3 } } process { foreach ($curNumber in $Number) { [int[]] $digits = $curNumber.ToString().PadLeft(4, '0').ToCharArray() | ForEach-Object { [Char]::GetNumericValue($_) } $RomanNumeral = '' $RomanNumeral += $DecimalToRoman.Thousands[$digits[$column.Thousands]] $RomanNumeral += $DecimalToRoman.Hundreds[$digits[$column.Hundreds]] $RomanNumeral += $DecimalToRoman.Tens[$digits[$column.Tens]] $RomanNumeral += $DecimalToRoman.Ones[$digits[$column.Ones]] if ($IncludeInput) { New-Object -TypeName psobject -Property ([ordered] @{ Number = $curNumber RomanNumeral = $RomanNumeral }) # write-output 'blah' } else { $RomanNumeral } } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |