Lightweight, dependency-free utility to convert numbers to ordinal strings.
npm install ordinal-number



Lightweight, dependency-free utility to convert numbers to their ordinal strings: 1 -> 1st, 2 -> 2nd, 3 -> 3rd, …
``bash`
npm i ordinal-numberor
pnpm add ordinal-numberor
yarn add ordinal-number
ESM:
`ts
import ordinal from 'ordinal-number';
ordinal(1); // "1st"
ordinal(2); // "2nd"
`
CommonJS:
`js
const ordinal = require('ordinal-number');
ordinal(3); // "3rd"
ordinal(11); // "11th"
`
TypeScript:
`ts
import { ordinal } from 'ordinal-number';
const out: string = ordinal(22);
`
#### ordinal(input: number | bigint | string): string
Converts an integer-like input into a string with an ordinal suffix. Handles:
- 0: 0th-1st
- Negatives: , -2nd, -3rd, -11thbigint
- Very large integers: via or numeric strings
- Strings: numeric strings are parsed; other inputs are stringified as-is
`ts``
ordinal(0); // "0th"
ordinal(-1); // "-1st"
ordinal(11); // "11th"
ordinal(12); // "12th"
ordinal(13); // "13th"
ordinal(111); // "111th"
ordinal(112); // "112th"
ordinal(113); // "113th"
ordinal(101); // "101st"
ordinal(-22); // "-22nd"
ordinal(12345678901234567890n); // "12345678901234567890th"
ordinal('42'); // "42nd"
ordinal('foo'); // "foo" (non-numeric strings are returned stringified)
- Dependency-free and tiny
- Dual ESM/CJS with TypeScript types
- Robust edge-case handling
See CONTRIBUTING.md.
ISC © Amit