Skip to content

Instantly share code, notes, and snippets.

@markwragg
Created April 3, 2017 09:44
Show Gist options
  • Save markwragg/8c24a6462feee7849ba7e06b0b0782fa to your computer and use it in GitHub Desktop.
Save markwragg/8c24a6462feee7849ba7e06b0b0782fa to your computer and use it in GitHub Desktop.
PowerShell function to add the appropriate ordinal number suffix to an integer for any number and return the number and suffix. Based on: http://stackoverflow.com/questions/19501722/how-to-convert-a-date-string-with-ordinal-suffix-to-another-format
Function Get-OrdinalNumber {
Param(
[Parameter(Mandatory=$true)]
[int64]$num
)
$Suffix = Switch -regex ($Num) {
'1(1|2|3)$' { 'th'; break }
'.?1$' { 'st'; break }
'.?2$' { 'nd'; break }
'.?3$' { 'rd'; break }
default { 'th'; break }
}
Write-Output "$Num$Suffix"
}
@tonypags
Copy link

tonypags commented Sep 23, 2018

Hi, I found a bug, just FYI
`PS ..\H:\

Get-OrdinalNumber 911
911th
PS ..\H:
Get-OrdinalNumber 912
912th`

It works OK if you just remove this one line:
'1(1|2|3)$' { 'th'; break }

Edit: Also I am going to rename this function in my fork to "Add-OrdinalSuffix" FWIW

@eamonburns
Copy link

@tonypags I think that is a feature. "nine hundred eleventh", and "nine hundred twelfth", not "nine hundred tenty first", or "nine hundred tenty second"

@tonypags
Copy link

How silly of 2018-me to suggest otherwise ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment