-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConvert-Int64ToText.ps1
107 lines (98 loc) · 3.59 KB
/
Convert-Int64ToText.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function Convert-Int64ToText {
<#
.SYNOPSIS
Convert integer to English text
.DESCRIPTION
Convert integer to English text
.PARAMETER Number
An [int64] value or an array of [int64]
.PARAMETER IncludeInput
Switch to enable the original datetime to appear in the output. Aliased to 'IncludeOriginal'
.EXAMPLE
Convert-Int64ToText -Number 13987
Thirteen Thousand Nine Hundred Eighty Seven
.EXAMPLE
Convert-Int64ToText -Number 1999, 2001, 2022 -IncludeInput
Number Result
------ ------
1999 One Thousand Nine Hundred Ninety Nine
2001 Two Thousand One
2022 Two Thousand Twenty Two
.EXAMPLE
723, 4560 | Convert-Int64ToText -IncludeInput
Number Result
------ ------
723 Seven Hundred Twenty Three
4560 Four Thousand Five Hundred Sixty
#>
[cmdletbinding()]
[alias('Convert-IntToText')] #FunctionAlias
param (
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
[int64[]] $Number,
[switch] $IncludeInput
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
Write-Verbose -Message "Number [$Number] IncludeInput [$IncludeInput]"
if (($Number -lt -999999999999) -or ($Number -gt 999999999999)) {
Write-Error -Message '-Number too large or too small. The absolute value of the number must be less than or equal to 999,999,999,999'
break
}
$class = @'
public class PFNum2Word
{
public static string NumberToText( long n)
{
if ( n < 0 )
return "Minus " + NumberToText(-n);
else if ( n == 0 )
return "";
else if ( n <= 19 )
return new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"}[n-1] + " ";
else if ( n <= 99 )
return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"}[n / 10 - 2] + " " + NumberToText(n % 10);
else if ( n <= 199 )
return "One Hundred " + NumberToText(n % 100);
else if ( n <= 999 )
return NumberToText(n / 100) + "Hundred " + NumberToText(n % 100);
else if ( n <= 1999 )
return "One Thousand " + NumberToText(n % 1000);
else if ( n <= 999999 )
return NumberToText(n / 1000) + "Thousand " + NumberToText(n % 1000);
else if ( n <= 1999999 )
return "One Million " + NumberToText(n % 1000000);
else if ( n <= 999999999)
return NumberToText(n / 1000000) + "Million " + NumberToText(n % 1000000);
else if ( n <= 1999999999 )
return "One Billion " + NumberToText(n % 1000000000);
else if ( n <= 999999999999 )
return NumberToText(n / 1000000000) + "Billion " + NumberToText(n % 1000000000);
else
return "";
}
}
'@
Add-Type -TypeDefinition $class
}
process {
foreach ($curNumber in $Number) {
$Result = ([PFNum2Word]::NumberToText($curNumber))
$Result = $Result.Trim()
if ($IncludeInput) {
New-Object -TypeName pscustomobject -Property ([ordered] @{
Number = $curNumber
Result = $Result
})
} else {
Write-Output -InputObject $Result
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
} # EndFunction Convert-Int64ToText