ASP.NET 中是否有标准格式字符串可将 1/2/3/... 转换为 1st/2nd/3rd...?

发布于 2024-09-03 03:19:53 字数 336 浏览 5 评论 0原文

我在 Access 数据库中有一个整数,该整数正在 ASP.NET 中显示。整数代表运动员在体育赛事中获得的位置(第一、第二、第三等),我想用标准后缀(如适当的“st”、“nd”、“rd”来显示它) ,而不仅仅是一个赤裸裸的数字。

一个重要的限制是,这是针对指定不编写 VB 或 C# 代码的赋值(实际上,它指示完全删除文件后面的代码)。理想情况下,我想使用标准格式字符串(如果可用),否则可能使用自定义字符串(我没有太多使用格式字符串,并且这没有足够高的优先级来投入大量时间*,但我很好奇关于是否有一个标准字符串)。

(*作业将于今晚到期,我已经惨痛地认识到,我不能把时间花在没有得到分数的事情上,即使它们让我非常恼火。)

I have an integer in an Access database, which is being displayed in ASP.NET. The integer represents the position achieved by a competitor in a sporting event (1st, 2nd, 3rd, etc.), and I'd like to display it with a standard suffix like 'st', 'nd', 'rd' as appropriate, rather than just a naked number.

An important limitation is that this is for an assignment which specifies that no VB or C# code be written (in fact it instructs code behind files to be deleted entirely). Ideally I'd like to use a standard format string if available, otherwise perhaps a custom string (I haven't worked with format strings much, and this isn't high enough priority to dedicate significant time to*, but I am very curious about whether there's a standard string for this).

(* The assignment is due tonight, and I've learned the hard way that I can't afford to spend time on things that don't get the marks, even if they irk me significantly.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-09-10 03:19:53

不幸的是,没有标准格式字符串可以做到这一点。但写起来并不难:

public static string ToOrdinal(this int i, string format)
{
   string suffix = "th";
   switch (i%100)
   {
       case 11:
       case 12:
       case 13:
          //deliberately empty
          break;
       default:
          switch (i%10)
          {
              case 1:
                  suffix = "st";
                  break;
              case 2:
                  suffix = "nd";
                  break;
              case 3:
                  suffix = "rd";
                  break;
           }
           break;
   }
   return i.ToString(format) + suffix;
}

Unfortunately, there is no standard format string to do this. But it's not that hard to write:

public static string ToOrdinal(this int i, string format)
{
   string suffix = "th";
   switch (i%100)
   {
       case 11:
       case 12:
       case 13:
          //deliberately empty
          break;
       default:
          switch (i%10)
          {
              case 1:
                  suffix = "st";
                  break;
              case 2:
                  suffix = "nd";
                  break;
              case 3:
                  suffix = "rd";
                  break;
           }
           break;
   }
   return i.ToString(format) + suffix;
}
芯好空 2024-09-10 03:19:53

你可以试试这个

public static string FormatOrdinalNumber(int number)
{
    if (number == 0) return "0";
    switch (number % 100)
    {
        case 11: 
        case 12: 
        case 13: 
        return number + "th";
    }
    switch (number % 10)
    {
        case 1: return number + "st";
        case 2: return number + "nd";
        case 3: return number + "rd";
    }
    return number + "th";
}

You could try this

public static string FormatOrdinalNumber(int number)
{
    if (number == 0) return "0";
    switch (number % 100)
    {
        case 11: 
        case 12: 
        case 13: 
        return number + "th";
    }
    switch (number % 10)
    {
        case 1: return number + "st";
        case 2: return number + "nd";
        case 3: return number + "rd";
    }
    return number + "th";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文