Search code examples
flutterdart

How to get ordinal Numbers in flutter


I tried getting some help from stack and l can across this Python code

I would like to know how to convert this into dart and use it to get ordinal numbers

ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n//10%10!=1)*(n%10<4)*n%10::4])

Solution

  • I made a package for this using the native platform libraries.

    https://pub.dev/packages/ordinal_formatter

    final ordinalString = await OrdinalFormatter().format(2) ?? ''; 
    // -> 2nd
    
    final localisedOrdinalString = await OrdinalFormatter().format(2, 'en_US') ?? ''; 
    // -> 2nd