다음을 변환하기 위해 .NET에서 누락 된 메소드 또는 형식 문자열이 있는지 궁금합니다.
1 to 1st
2 to 2nd
3 to 3rd
4 to 4th
11 to 11th
101 to 101st
111 to 111th
이 링크 자신의 함수를 작성하는 데 관련된 기본 원칙에 대한 나쁜 예가 있지만 누락 된 용량이 있으면 더 궁금합니다.
솔루션
Scott Hanselman의 답변은 질문에 직접 답변하기 때문에 받아 들여지는 답변입니다.
그러나 해결책은 이 위대한 대답 을 참조하십시오.
아니요. .NET 기본 클래스 라이브러리에는 기본 제공 기능이 없습니다.
생각보다 훨씬 간단한 기능입니다. 이미 존재하는 .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;
}
간단하고 깨끗하며 빠릅니다.
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();
@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);
}
이것은 이미 다루었지만 링크하는 방법을 잘 모르겠습니다. 다음은 코드 스 니핏입니다.
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보다 작은 경우 this 키워드를 제거하십시오.
[편집] : 잘못되었다는 점을 지적 해 주셔서 감사합니다. 복사/붙여 넣기 코드는 다음과 같습니다. :)
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
나는 이것이 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;
..., -1st, 0th가 의미가 있습니까?
또 다른 맛 :
/// <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";
}
}
}
}
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";
}