Skip to main content

Ordinal

This code snippet gets the ordinal corresponding to a given integer. For example, the ordinal of 1 would be st, nd for 122, and so on.

Licensed under the terms of the Unlicense.

tip

This may be useful for join feeds - showing You're the 5th member is nicer than You're member #5.

Code

{{/*
Computes the ordinal for an integer:
1 -> 1st, 11 -> 11th, 122 -> 122nd.
See <https://yagpdb-cc.github.io/code-snippets/ordinal> for more information.

Licensed under the terms of the Unlicense.
Author: jo3-l <https://github.com/jo3-l>
*/}}

{{/* Let $int be the integer. */}}
{{ $int := 123 }}

{{ $ord := "th" }}
{{ $cent := toInt (mod $int 100) }}
{{ $dec := toInt (mod $int 10) }}
{{ if not (and (ge $cent 10) (le $cent 19)) }}
{{ if eq $dec 1 }} {{ $ord = "st" }}
{{ else if eq $dec 2 }} {{ $ord = "nd" }}
{{ else if eq $dec 3 }} {{ $ord = "rd" }}
{{ end }}
{{ end }}

Usage

First, add the code snippet:

{{/* code snippet goes here */}}

Next, change the value of $int to what you want:

{{/* Let $int be the integer. */}}
- {{ $int := 123 }}
+ {{ $int := .Guild.MemberCount }}
{{/* Rest of snippet goes here */}}

And that's it! $ord will be the ordinal for the integer $int.

{{/* code snippet goes here */}}
You are the {{$int}}{{$ord}} member!

Author

This code snippet was written by @jo3-l.