Skip to content

Instantly share code, notes, and snippets.

@michaelvdnest
Created May 22, 2015 10:17
Show Gist options
  • Save michaelvdnest/bd5a3a9552fadea9ddfb to your computer and use it in GitHub Desktop.
Save michaelvdnest/bd5a3a9552fadea9ddfb to your computer and use it in GitHub Desktop.
Format a number as a word
function Format-NumAsWord
{
[CmdletBinding()]
param (
[parameter(Position=0)]
[double]$n = 0,
[parameter(Position=1)]
[boolean]$Caps = $False
)
#not catering for currency for decimals yet
$numbers = 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight',
'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen',
'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'
$tens = 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'
$suffixes = 'Thousand' , 'Million' , 'Billion' , 'Trillion' ,
'Quadrillion' , 'Quintillion' , 'Sextillion' ,
'Septillion' , 'Octillion' , 'Nonillion' ,
'Decillion' , 'Undecillion' , 'Duodecillion' ,
'Tredecillion' , 'Quattuordecillion' , 'Quinquadecillion' ,
'Sedecillion' , 'Septendecillion' , 'Octodecillion' ,
'Novendecillion' , 'Vigintillion' , 'Unvigintillion' ,
'Duovigintillion' , 'Tresvigintillion' , 'Quattuorvigintillion',
'Quinquavigintillion' , 'Sesvigintillion' , 'Septemvigintillion' ,
'Octovigintillion' , 'Novemvigintillion' , 'Trigintillion' ,
'Untrigintillion' , 'Duotrigintillion' , 'Trestrigintillion' ,
'Quattuortrigintillion', 'Quinquatrigintillion', 'Sestrigintillion' ,
'Septentrigintillion' , 'Octotrigintillion' , 'Noventrigintillion' , 'Quadragintillion'
$hasTens = $False
$words = ''
$word = ''
if ($n -lt 0){
$words = 'Negative'
$n *= -1
}
$power = ($suffixes.Length + 1) * 3
while ($power -gt 3){
$pow = [Math]::Pow(10, $power)
if ($n -gt $pow){
if ($n % [Math]::Pow(10, $power) -gt 0){
$words += Format-NumAsWord([Math]::Floor($n / $pow))
$words += " "
$words += $suffixes[($power / 3) - 1]
$words += " "
}
elseif ($n % $pow -gt 0){
$words += Format-NumAsWord([Math]::Floor($n / $pow))
$words += " "
$words += $suffixes[($power / 3) - 1]
}
$n %= $pow;
}
$power -= 3
}
if ($n -ge 1000){
if ($n % 1000 > 0) {
$words += Format-NumAsWord([Math]::Floor($n / 1000))
$words += " Thousand "
}
else {
$word += Format-NumAsWord([Math]::Floor($n / 1000))
$words += "$word Thousand "
$n %= 1000;
}
}
if (0 -le $n -and $n -le 999) {
if ([Math]::Floor($n / 100) -gt 0)
{
$word = Format-NumAsWord([Math]::Floor($n / 100))
$words += "$word Hundred"
$n %= 100;
}
if ([Math]::Floor($n / 10) -gt 1)
{
if ($words -ne ""){ $words += " " }
$words += $tens[[Math]::Floor($n / 10) - 2]
$hasTens = $True;
$n %= 10;
}
if ($n -lt 20 -and $n -gt 0)
{
if ($words -ne "" -and $hasTens -eq $False) { $words += " " }
if ($hasTens) {
$words += "-"
$words += $numbers[[int]$n - 1]
}
else {
$words += $numbers[[int]$n - 1]
}
$n -= [Math]::Floor($n)
}
if ($Caps) {
$words = $words.ToUpper()
}
return $words
}
}
Format-NumAsWord 1
#One
Format-NumAsWord 2
#Two
Format-NumAsWord 123
#One Hundred Twenty-Three
Format-NumAsWord 32134645
Thirty-Two Million One Hundred Thirty-Four Thousand Six Hundred Forty-Five
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment