Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Appends ordinal (st, nd ,rd ,th) to number or turns number into ordinal. Typescript Angular2 Pipe
import { Pipe, PipeTransform } from '@angular/core';
const ordinals: string[] = ['th','st','nd','rd'];
/*
* Append ordinal to number (e.g. "1st" position)
* Usage:
* value | ordinal:keepNumber
* Example:
* {{ 23 | ordinal}}
* formats to: '23rd'
* Example:
* {{ 23 | ordinal:false}}
* formats to: 'rd'
*/
@Pipe({name: 'ordinal'})
export class OrdinalPipe implements PipeTransform {
transform(n: number, keepNumber: boolean = true) {
let v = n % 100;
return (keepNumber?n:'') + (ordinals[(v-20)%10]||ordinals[v]||ordinals[0]);
}
}
@rynop

This comment has been minimized.

Copy link

@rynop rynop commented Nov 16, 2018

thank you sir!

@walkerrabelo

This comment has been minimized.

Copy link

@walkerrabelo walkerrabelo commented Jun 13, 2019

Simple and effective ! Thank You !

@spahicharis

This comment has been minimized.

Copy link

@spahicharis spahicharis commented Jul 10, 2019

Thnx 👍

@edencraig

This comment has been minimized.

Copy link

@edencraig edencraig commented Aug 7, 2019

Perfect. Thank you

@battulasurendra

This comment has been minimized.

Copy link

@battulasurendra battulasurendra commented Jan 6, 2020

Really good. Thank You

@gvdm90

This comment has been minimized.

Copy link

@gvdm90 gvdm90 commented Jan 21, 2020

Great! Thank you very much

@ShaneMcGowan

This comment has been minimized.

Copy link

@ShaneMcGowan ShaneMcGowan commented Apr 4, 2020

Great stuff, thanks for sharing!

@ShrinivasKattimani

This comment has been minimized.

Copy link

@ShrinivasKattimani ShrinivasKattimani commented May 29, 2020

Thanks a ton man. It saved a light year for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.