.NET 中是否有一种简单的方法来获取数字的“st”、“nd”、“rd”和“th”结尾?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/69262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an easy way in .NET to get "st", "nd", "rd" and "th" endings for numbers?
提问by Matt Mitchell
I am wondering if there is a method or format string I'm missing in .NET to convert the following:
我想知道 .NET 中是否缺少一种方法或格式字符串来转换以下内容:
1 to 1st
2 to 2nd
3 to 3rd
4 to 4th
11 to 11th
101 to 101st
111 to 111th
This linkhas a bad example of the basic principle involved in writing your own function, but I am more curious if there is an inbuilt capacity I'm missing.
这个链接有一个关于编写自己的函数所涉及的基本原则的坏例子,但我更好奇是否有我缺少的内置容量。
Solution
解决方案
Scott Hanselman's answer is the accepted one because it answers the question directly.
Scott Hanselman 的答案是公认的,因为它直接回答了这个问题。
For a solution however, see this great answer.
但是,对于解决方案,请参阅这个很好的答案。
采纳答案by Scott Hanselman
No, there is no inbuilt capability in the .NET Base Class Library.
不,.NET 基类库中没有内置功能。
回答by nickf
It's a function which is a lot simpler than you think. Though there might be a .NET function already in existence for this, the following function (written in PHP) does the job. It shouldn't be too hard to port it over.
这是一个比你想象的要简单得多的函数。虽然可能已经存在一个 .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;
}
回答by Shahzad Qureshi
Simple, clean, quick
简单、干净、快速
private static string GetOrdinalSuffix(int num)
{
string number = num.ToString();
if (number.EndsWith("11")) return "th";
if (number.EndsWith("12")) return "th";
if (number.EndsWith("13")) return "th";
if (number.EndsWith("1")) return "st";
if (number.EndsWith("2")) return "nd";
if (number.EndsWith("3")) return "rd";
return "th";
}
Or better yet, as an extension method
或者更好的是,作为一种扩展方法
public static class IntegerExtensions
{
public static string DisplayWithSuffix(this int num)
{
string number = num.ToString();
if (number.EndsWith("11")) return number + "th";
if (number.EndsWith("12")) return number + "th";
if (number.EndsWith("13")) return number + "th";
if (number.EndsWith("1")) return number + "st";
if (number.EndsWith("2")) return number + "nd";
if (number.EndsWith("3")) return number + "rd";
return number + "th";
}
}
Now you can just call
现在你可以打电话
int a = 1;
a.DisplayWithSuffix();
or even as direct as
甚至直接
1.DisplayWithSuffix();
回答by Scott Dorman
@nickf: Here is the PHP function in C#:
@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);
}
回答by mjallday
This has already been covered but I'm unsure how to link to it. Here is the code 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";
}
}
FYI: This is as an extension method. If your .NET version is less than 3.5 just remove the this keyword
仅供参考:这是一种扩展方法。如果您的 .NET 版本低于 3.5 只需删除 this 关键字
[EDIT]: Thanks for pointing that it was incorrect, that's what you get for copy / pasting code :)
[编辑]:感谢您指出它不正确,这就是您复制/粘贴代码所得到的:)
回答by redcalx
Here's a Microsoft SQL Server Function version:
这是 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
回答by avenmore
I know this isn't an answer to the OP's question, but because I found it useful to lift the SQL Server function from this thread, here is a Delphi (Pascal) equivalent:
我知道这不是 OP 问题的答案,但是因为我发现从该线程中提取 SQL Server 函数很有用,所以这里有一个 Delphi (Pascal) 等效项:
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;
Does ..., -1st, 0th make sense?
..., -1st, 0th 有意义吗?
回答by Frank Hoffman
Another flavor:
另一种口味:
/// <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";
}
}
}
}
回答by Faust
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";
}
回答by Faust
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;

