2008-09-16 19 views
59

我想知道是否有我缺少在.NET轉換下面的方法或格式字符串:有沒有一種簡單的方法在.NET中獲得數字的「st」,「nd」,「rd」和「th」結尾?

1 to 1st 
    2 to 2nd 
    3 to 3rd 
    4 to 4th 
    11 to 11th 
101 to 101st 
111 to 111th 

This link有參與編寫自己的功能的基本原理了一個壞榜樣,但我如果我缺少內置容量,我會更好奇。

解決方案

斯科特Hanselman的答案是公認的一個,因爲它直接回答了這個問題。

但是,有關解決方案,請參閱this great answer

+3

他們被稱爲序數(第一,第二,等等),而不是基數(1,2,3等),僅供參考。 – pc1oad1etter 2008-09-16 04:15:10

+1

這是在這裏回答很優雅:http://stackoverflow.com/questions/20156/ordinals-in-c# – Portman 2008-09-17 14:32:17

+0

是的,我注意到你的答案。 – 2008-09-18 00:41:35

回答

53

不,.NET基類庫中沒有內置功能。

+8

這是一個遲到的評論,但是有沒有計劃將此功能添加到.NET BCL中以格式化日期? – Breakskater 2013-05-19 16:09:41

79

這是一個比您想象的要簡單得多的功能。雖然可能有一個.NET函數已經存在,但以下函數(用PHP編寫)可以完成這項工作。移植它不應該太難。

function ordinal($num) { 
    $ones = $num % 10; 
    $tens = floor($num/10) % 10; 
    if ($tens == 1) { 
     $suff = "th"; 
    } else { 
     switch ($ones) { 
      case 1 : $suff = "st"; break; 
      case 2 : $suff = "nd"; break; 
      case 3 : $suff = "rd"; break; 
      default : $suff = "th"; 
     } 
    } 
    return $num . $suff; 
} 
+0

本地化怎麼樣? – macbirdie 2008-09-16 21:26:59

+2

本地化將意味着您必須爲每種語言創建單獨的函數。在德語中,您可以追加「ter」,但「1ter」「2ter」「3ter」看起來確實很糟糕,即使它在語法上是正確的。用法語說得好一點,但對於每種語言都沒有通用的方法。 – 2008-09-17 09:14:49

+0

@Michael Stum:我對所有的國際序數格式都不太熟悉,但是string.Format(resString,number)就足夠了嗎?或者是否有些語言不會將數字與序數(前/後綴)ixes結合? – 2010-06-21 05:15:23

12

這已經被覆蓋,但我不確定如何鏈接到它。這裏是代碼snippit:

public static string Ordinal(this int number) 
    { 
     var ones = number % 10; 
     var tens = Math.Floor (number/10f) % 10; 
     if (tens == 1) 
     { 
      return number + "th"; 
     } 

     switch (ones) 
     { 
      case 1: return number + "st"; 
      case 2: return number + "nd"; 
      case 3: return number + "rd"; 
      default: return number + "th"; 
     } 
    } 

供參考:這是一個擴展方法。如果你的.NET版本低於3.5只是刪除了這個關鍵字

[編輯]:感謝您指出這是不正確的,那就是你的複製/粘貼代碼:)

+4

不起作用。 1011%10 == 1. 1011st不正確。 – 2008-09-16 04:00:48

-6

我覺得序後綴很難得到......你基本上必須編寫一個使用開關來測試數字並添加後綴的函數。

語言沒有理由在內部提供這種語言,特別是在語言環境特定的情況下。

你可以做一點比當涉及到的代碼編寫量鏈接更好,但你必須編寫此功能...

54

@nickf:這是在C#中的PHP函數:

public static string Ordinal(int number) 
{ 
    string suffix = String.Empty; 

    int ones = number % 10; 
    int tens = (int)Math.Floor(number/10M) % 10; 

    if (tens == 1) 
    { 
     suffix = "th"; 
    } 
    else 
    { 
     switch (ones) 
     { 
      case 1: 
       suffix = "st"; 
       break; 

      case 2: 
       suffix = "nd"; 
       break; 

      case 3: 
       suffix = "rd"; 
       break; 

      default: 
       suffix = "th"; 
       break; 
     } 
    } 
    return String.Format("{0}{1}", number, suffix); 
} 
-3
else if (choice=='q') 
{ 
    qtr++; 

    switch (qtr) 
    { 
     case(2): strcpy(qtrs,"nd");break; 
     case(3): 
     { 
      strcpy(qtrs,"rd"); 
      cout<<"End of First Half!!!"; 
      cout<<" hteam "<<"["<<hteam<<"] "<<hs; 
      cout<<" vteam "<<" ["<<vteam; 
      cout<<"] "; 
      cout<<vs;dwn=1;yd=10; 

      if (beginp=='H') team='V'; 
      else    team='H'; 
      break; 
     } 
     case(4): strcpy(qtrs,"th");break; 
8

這裏有一個Microsoft SQL Server的功能版本:

CREATE FUNCTION [Internal].[GetNumberAsOrdinalString] 
(
    @num int 
) 
RETURNS nvarchar(max) 
AS 
BEGIN 

    DECLARE @Suffix nvarchar(2); 
    DECLARE @Ones int; 
    DECLARE @Tens int; 

    SET @Ones = @num % 10; 
    SET @Tens = FLOOR(@num/10) % 10; 

    IF @Tens = 1 
    BEGIN 
     SET @Suffix = 'th'; 
    END 
    ELSE 
    BEGIN 

    SET @Suffix = 
     CASE @Ones 
      WHEN 1 THEN 'st' 
      WHEN 2 THEN 'nd' 
      WHEN 3 THEN 'rd' 
      ELSE 'th' 
     END 
    END 

    RETURN CONVERT(nvarchar(max), @num) + @Suffix; 
END 
2

我知道這是不是一個答案任擇議定書的問題,而是因爲我認爲應該從這個線程解除SQL Server的功能,這裏是一個Delphi(帕斯卡)相當於:

function OrdinalNumberSuffix(const ANumber: integer): string; 
begin 
    Result := IntToStr(ANumber); 
    if(((Abs(ANumber) div 10) mod 10) = 1) then // Tens = 1 
    Result := Result + 'th' 
    else 
    case(Abs(ANumber) mod 10) of 
     1: Result := Result + 'st'; 
     2: Result := Result + 'nd'; 
     3: Result := Result + 'rd'; 
     else 
     Result := Result + 'th'; 
    end; 
end; 

是否...,第-1,第0有意義嗎?

50

簡單,乾淨,快捷

private static string GetOrdinalSuffix(int num) 
    { 
     if (num.ToString().EndsWith("11")) return "th"; 
     if (num.ToString().EndsWith("12")) return "th"; 
     if (num.ToString().EndsWith("13")) return "th"; 
     if (num.ToString().EndsWith("1")) return "st"; 
     if (num.ToString().EndsWith("2")) return "nd"; 
     if (num.ToString().EndsWith("3")) return "rd"; 
     return "th"; 
    } 

或者更好的是,作爲一個擴展方法

public static class IntegerExtensions 
{ 
    public static string DisplayWithSuffix(this int num) 
    { 
     if (num.ToString().EndsWith("11")) return num.ToString() + "th"; 
     if (num.ToString().EndsWith("12")) return num.ToString() + "th"; 
     if (num.ToString().EndsWith("13")) return num.ToString() + "th"; 
     if (num.ToString().EndsWith("1")) return num.ToString() + "st"; 
     if (num.ToString().EndsWith("2")) return num.ToString() + "nd"; 
     if (num.ToString().EndsWith("3")) return num.ToString() + "rd"; 
     return num.ToString() + "th"; 
    } 
} 

現在你可以叫

int a = 1; 
a.DisplayWithSuffix(); 

,甚至直接爲

1.DisplayWithSuffix(); 
0

另一種味道:

/// <summary> 
/// Extension methods for numbers 
/// </summary> 
public static class NumericExtensions 
{ 
    /// <summary> 
    /// Adds the ordinal indicator to an integer 
    /// </summary> 
    /// <param name="number">The number</param> 
    /// <returns>The formatted number</returns> 
    public static string ToOrdinalString(this int number) 
    { 
     // Numbers in the teens always end with "th" 

     if((number % 100 > 10 && number % 100 < 20)) 
      return number + "th"; 
     else 
     { 
      // Check remainder 

      switch(number % 10) 
      { 
       case 1: 
        return number + "st"; 

       case 2: 
        return number + "nd"; 

       case 3: 
        return number + "rd"; 

       default: 
        return number + "th"; 
      } 
     } 
    } 
} 
0
public static string OrdinalSuffix(int ordinal) 
{ 
    //Because negatives won't work with modular division as expected: 
    var abs = Math.Abs(ordinal); 

    var lastdigit = abs % 10; 

    return 
     //Catch 60% of cases (to infinity) in the first conditional: 
     lastdigit > 3 || lastdigit == 0 || (abs % 100) - lastdigit == 10 ? "th" 
      : lastdigit == 1 ? "st" 
      : lastdigit == 2 ? "nd" 
      : "rd"; 
} 
相關問題