在.NET中有没有一种简单的方法来获取;",&引用;“第&引用;rd";及;";数字的结尾?
我想知道.NET中是否缺少用于转换以下内容的方法或格式字符串:在.NET中有没有一种简单的方法来获取;",&引用;“第&引用;rd";及;";数字的结尾?,.net,formatting,date,ordinals,.net,Formatting,Date,Ordinals,我想知道.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基类库中没有内置功能。这是一个比您想象的简单得
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;
}
这已经涵盖,但我不确定如何链接到它。以下是代码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,只需删除this关键字
[编辑]:感谢您指出它不正确,这就是复制/粘贴代码得到的结果:)我认为序号后缀很难获得。。。您基本上必须编写一个函数,该函数使用开关来测试数字并添加后缀 语言没有理由在内部提供这种功能,特别是在特定于语言环境的情况下
当涉及到要编写的代码量时,您可以比该链接做得更好,但您必须为此编写一个函数…@nickf:C#中的PHP函数如下:
else if(选项=='q')
{
qtr++;
开关(qtr)
{
案例(2):strcpy(qtrs,“nd”);断裂;
案例(3):
{
strcpy(qtrs,“rd”);
cout这是一个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”有意义吗?简单、干净、快速
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";
}
或者更好,作为一种扩展方法
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";
}
}
现在你可以打电话了
int a = 1;
a.DisplayWithSuffix();
甚至是直接的
1.DisplayWithSuffix();
另一种味道:
/// <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";
}
}
}
}
//
///数字的扩展方法
///
公共静态类NumericExtensions
{
///
///将序号指示符添加到整数
///
///号码
///格式化的数字
公共静态字符串ToOrdinalString(此整数)
{
//青少年的数字总是以“th”结尾
如果((编号%100>10和编号%100<20))
返回编号+第次;
其他的
{
//检查余数
开关(编号%10)
{
案例1:
返回编号+st;
案例2:
返回编号+nd;
案例3:
返回号码+“rd”;
违约:
返回编号+第次;
}
}
}
}
不起作用。1011%10==1.1011是不正确的。我喜欢你声明ones变量而从不使用它的方式。@MattMitchell在你的例子中,它应该是10110而不是101101011st@EOLeary我不知道你在说什么,但我认为马歇尔的编辑符合我的例子。为什么使用浮点而不是纯整数?我会使用(number/10)%10
。考虑到所有货币本地化字符串等,添加序数后缀似乎有点牵强。它们被称为序数(1、2等),而不是基数(1、2、3等),仅供参考。这里的回答相当优雅:哈,谢谢,就要发布我写的代码了。你的字符串格式比我的好。我想。1)为什么要转换为十进制?一个简单的(数字/10)%10
会起作用。2)为什么要将后缀
初始化为一个永远不会使用的值?@code混沌:如果不转换为十进制,则会出现编译器错误:以下方法或属性之间的调用不明确:“System.Math.Floor(decimal)”和“System.Math.Floor(double)”“
。初始化后缀
为字符串。空
通常是一种习惯,但也有助于避免意外使用未赋值局部变量“后缀”
错误。@ScottDorman 1)只有在调用Floor
时,这对整数没有意义。整数除法简单地向零截断,不需要ca按小数点排列或使用楼层(数字/10)%10
更简单且有效。2)如果您忽略了代码路径,则会发生这些错误。编译器错误会告诉您修复该错误,而不是默默地返回无用的值。本地化如何?本地化意味着您必须为每种语言创建单独的函数。在德语中,您可以只添加“ter”,但“1ter”和“2ter”“3ter”看起来很糟糕,尽管它语法正确。法语有点好,但是没有任何一种通用的语言。@ MichaelStum:实际上,在德语中你不能只添加“Tter”。考虑“Heut-ister-1TeJuaar”(今天是一月的第一)。或者“KLIKEN SIE DEND510按钮”(点击第五按钮)。举两个例子。你必须考虑每一次使用的正确的弯曲(Eng.Engress)。在德语中组合一个数字和这样的后缀是不寻常的。你可以把它写为“1”或“Errest/”。"。后者通常用于文本,很少需要自动生成。@dannio不,有一个条件是$tens
我几乎一字不差地写了这个函数!区别:master db,cast而不是convert,我使用稍微不同的缩进。我想是伟大的头脑…+1-只是需要一个SQL版本-保存me编写OneScope非常好,但仅当我们从SQL获取时。但在本例中,我正在格式化一个.net DateTime变量。但此函数将非常有用。这是一个很晚的注释,但是否有计划将此功能添加到.net BCL以格式化d
1.DisplayWithSuffix();
/// <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";
}