| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="EnglishOrdinalizer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Globalization.Culture; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Humanizer.Ordinalizing.Cultures; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents an ordinalizer for the English language, which converts numbers into their corresponding ordinal string r |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class EnglishOrdinalizer : OrdinalizerBase |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="EnglishOrdinalizer"/> class. |
| | | 19 | | /// </summary> |
| | | 20 | | internal EnglishOrdinalizer() |
| | 3 | 21 | | : base(SupportedCultures.English) |
| | | 22 | | { |
| | 3 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc/> |
| | 285 | 26 | | public override string Ordinalize(long number, OrdinalizationOptions? options = null) => $"{number}{GetSuffix(number |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Determines the appropriate suffix for a given number based on English ordinalization rules. The method calculate |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="number">The number for which to determine the ordinal suffix.</param> |
| | | 32 | | /// <returns>The appropriate ordinal suffix for the given number.</returns> |
| | | 33 | | private static string GetSuffix(long number) |
| | | 34 | | { |
| | 285 | 35 | | var absolute = Math.Abs(number); |
| | | 36 | | |
| | 285 | 37 | | var lastTwoDigits = absolute % 100; |
| | | 38 | | |
| | 285 | 39 | | return lastTwoDigits is >= 11 and <= 13 |
| | 285 | 40 | | ? "th" |
| | 285 | 41 | | : (absolute % 10) switch |
| | 285 | 42 | | { |
| | 60 | 43 | | 1 => "st", |
| | 39 | 44 | | 2 => "nd", |
| | 30 | 45 | | 3 => "rd", |
| | 129 | 46 | | _ => "th" |
| | 285 | 47 | | }; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |