Skip to content

Instantly share code, notes, and snippets.

@pmn4
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save pmn4/1c977e32b39f5bba7432 to your computer and use it in GitHub Desktop.

Select an option

Save pmn4/1c977e32b39f5bba7432 to your computer and use it in GitHub Desktop.
AngularJS Ordinal Filter
// convert numbers to ordinal versions of themselves
// {{ 1 | ordinal }} : "1st"
// {{ 2 | ordinal }} : "2nd"
// {{ 3 | ordinal }} : "3rd"
// etc.
angular
.module("app", [])
.filter("ordinal", function () {
var suffix = ["th", "st", "nd", "rd"];
return function (cardinal) {
var v = cardinal % 100;
// suffix[(v - 20) % 10]
// the teens are weird, so we ignore everything less than 20
// for numbers >= 20, we look at the one's digit
// suffix[v]
// this is just 0 - 3. numbers > 3 will be null
// suffix[0]
// 3 < n < 20
return cardinal + (suffix[(v - 20) % 10] || suffix[v] || suffix[0]);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment