Mat*_*ell 67 .net formatting ordinals date
我想知道是否有一个方法或格式字符串我在.NET中缺少转换以下内容:
1 to 1st
2 to 2nd
3 to 3rd
4 to 4th
11 to 11th
101 to 101st
111 to 111th
Run Code Online (Sandbox Code Playgroud)
这个链接有一个很好的例子,说明了编写自己的函数所涉及的基本原理,但是如果我缺少内置容量,我会更好奇.
解
斯科特汉塞尔曼的回答是公认的,因为它直接回答了这个问题.
但是,对于解决方案,请看这个很好的答案.
nic*_*ckf 84
这是一个比你想象的要简单得多的功能.虽然可能已经存在.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;
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*shi 59
简单,干净,快捷
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";
}
Run Code Online (Sandbox Code Playgroud)
或者更好,作为一种扩展方法
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";
}
}
Run Code Online (Sandbox Code Playgroud)
现在你可以打电话了
int a = 1;
a.DisplayWithSuffix();
Run Code Online (Sandbox Code Playgroud)
甚至像直接一样
1.DisplayWithSuffix();
Run Code Online (Sandbox Code Playgroud)
Sco*_*man 55
@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);
}
Run Code Online (Sandbox Code Playgroud)
mja*_*day 13
这已经被覆盖但我不确定如何链接到它.这是代码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";
}
}
Run Code Online (Sandbox Code Playgroud)
仅供参考:这是一种扩展方法.如果您的.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
Run Code Online (Sandbox Code Playgroud)