μΉ΄ν…Œκ³ λ¦¬ 보관물: C#

C#

C #μ—μ„œ μ„œμˆ˜λ₯Ό λ§Œλ“œλŠ” μ‰¬μš΄ 방법이 μžˆμŠ΅λ‹ˆκΉŒ? λ°˜ν™˜ 2λŠ” 2λ₯Ό λ°˜ν™˜ 3은 3을 λ°˜ν™˜ …기타 이 μž‘μ—…μ„ μˆ˜ν–‰

C #μ—μ„œ μˆ«μžμ— λŒ€ν•œ μ„œμˆ˜ λ₯Ό λ§Œλ“œλŠ” μ‰¬μš΄ 방법이 μžˆμŠ΅λ‹ˆκΉŒ? 예λ₯Ό λ“€λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  • 1은 1을 λ°˜ν™˜
  • 2λŠ” 2λ₯Ό λ°˜ν™˜
  • 3은 3을 λ°˜ν™˜
  • …기타

이 μž‘μ—…μ„ μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆκΉŒ String.Format()? μ•„λ‹ˆλ©΄μ΄λ₯Ό μˆ˜ν–‰ ν•  μˆ˜μžˆλŠ” κΈ°λŠ₯이 μžˆμŠ΅λ‹ˆκΉŒ?



λ‹΅λ³€

이 νŽ˜μ΄μ§€λŠ” λͺ¨λ“  μ‚¬μš©μž 지정 숫자 μ„œμ‹ κ·œμΉ™μ˜ 전체 λͺ©λ‘μ„ μ œκ³΅ν•©λ‹ˆλ‹€.

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

λ³΄μ‹œλ‹€μ‹œν”Ό μ„œμˆ˜μ— λŒ€ν•΄μ„œλŠ” 아무것도 μ—†μœΌλ―€λ‘œ String.Format을 μ‚¬μš©ν•˜μ—¬ μˆ˜ν–‰ ν•  수 μ—†μŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ‹€μ œλ‘œ κ·Έλ ‡κ²Œν•˜λŠ” κΈ°λŠ₯을 μž‘μ„±ν•˜λŠ” 것은 어렡지 μ•ŠμŠ΅λ‹ˆλ‹€.

public static string AddOrdinal(int num)
{
    if( num <= 0 ) return num.ToString();

    switch(num % 100)
    {
        case 11:
        case 12:
        case 13:
            return num + "th";
    }

    switch(num % 10)
    {
        case 1:
            return num + "st";
        case 2:
            return num + "nd";
        case 3:
            return num + "rd";
        default:
            return num + "th";
    }
}

μ—…λ°μ΄νŠΈ : 기술적으둜 μ„œμˆ˜λŠ” <= 0에 μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ μœ„ μ½”λ“œλ₯Ό μ—…λ°μ΄νŠΈν–ˆμŠ΅λ‹ˆλ‹€. 쀑볡 ToString()방법 도 μ œκ±°ν–ˆμŠ΅λ‹ˆλ‹€ .

λ˜ν•œ κ΅­μ œν™”λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. λ‚˜λŠ” λ‹€λ₯Έ μ–Έμ–΄μ—μ„œ μ„œ μˆ˜κ°€ μ–΄λ–»κ²Œ μƒκ²ΌλŠ”μ§€ μ „ν˜€ λͺ¨λ₯Έλ‹€.


λ‹΅λ³€

κ΅­μ œν™”λ₯Ό κΈ°μ–΅ν•˜μ‹­μ‹œμ˜€!

이 μ†”λ£¨μ…˜μ€ μ˜μ–΄λ‘œλ§Œ μž‘λ™ν•©λ‹ˆλ‹€. λ‹€λ₯Έ μ–Έμ–΄λ₯Ό 지원해야 ν•  경우 상황이 훨씬 λ³΅μž‘ν•΄μ§‘λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ μŠ€νŽ˜μΈμ–΄μ—μ„œ β€œ1stβ€λŠ” μ„ΈλŠ” 것이 남성적이든 여성적이든 λ³΅μˆ˜ν˜•μΈμ§€μ— 따라 β€œ1.o”, β€œ1.a”, β€œ1.osβ€λ˜λŠ” β€œ1.asβ€λ‘œ μž‘μ„±λ©λ‹ˆλ‹€. !

λ”°λΌμ„œ μ†Œν”„νŠΈμ›¨μ–΄κ°€ λ‹€λ₯Έ μ–Έμ–΄λ₯Ό μ§€μ›ν•΄μ•Όν•˜λŠ” 경우 μ„œμˆ˜λ₯Ό ν”Όν•˜μ‹­μ‹œμ˜€.


λ‹΅λ³€

Jesse λ²„μ „μ˜ Stu 및 samjudson 버전 πŸ™‚

숫자 <1 일 λ•Œ ν—ˆμš© 된 닡변이 잘λͺ»λ˜μ—ˆμŒμ„ ν‘œμ‹œν•˜κΈ°μœ„ν•œ λ‹¨μœ„ ν…ŒμŠ€νŠΈ 포함

    /// <summary>
    /// Get the ordinal value of positive integers.
    /// </summary>
    /// <remarks>
    /// Only works for english-based cultures.
    /// Code from: http://stackoverflow.com/questions/20156/is-there-a-quick-way-to-create-ordinals-in-c/31066#31066
    /// With help: http://www.wisegeek.com/what-is-an-ordinal-number.htm
    /// </remarks>
    /// <param name="number">The number.</param>
    /// <returns>Ordinal value of positive integers, or <see cref="int.ToString"/> if less than 1.</returns>
    public static string Ordinal(this int number)
    {
        const string TH = "th";
        string s = number.ToString();

        // Negative and zero have no ordinal representation
        if (number < 1)
        {
            return s;
        }

        number %= 100;
        if ((number >= 11) && (number <= 13))
        {
            return s + TH;
        }

        switch (number % 10)
        {
            case 1: return s + "st";
            case 2: return s + "nd";
            case 3: return s + "rd";
            default: return s + TH;
        }
    }

    [Test]
    public void Ordinal_ReturnsExpectedResults()
    {
        Assert.AreEqual("-1", (1-2).Ordinal());
        Assert.AreEqual("0", 0.Ordinal());
        Assert.AreEqual("1st", 1.Ordinal());
        Assert.AreEqual("2nd", 2.Ordinal());
        Assert.AreEqual("3rd", 3.Ordinal());
        Assert.AreEqual("4th", 4.Ordinal());
        Assert.AreEqual("5th", 5.Ordinal());
        Assert.AreEqual("6th", 6.Ordinal());
        Assert.AreEqual("7th", 7.Ordinal());
        Assert.AreEqual("8th", 8.Ordinal());
        Assert.AreEqual("9th", 9.Ordinal());
        Assert.AreEqual("10th", 10.Ordinal());
        Assert.AreEqual("11th", 11.Ordinal());
        Assert.AreEqual("12th", 12.Ordinal());
        Assert.AreEqual("13th", 13.Ordinal());
        Assert.AreEqual("14th", 14.Ordinal());
        Assert.AreEqual("20th", 20.Ordinal());
        Assert.AreEqual("21st", 21.Ordinal());
        Assert.AreEqual("22nd", 22.Ordinal());
        Assert.AreEqual("23rd", 23.Ordinal());
        Assert.AreEqual("24th", 24.Ordinal());
        Assert.AreEqual("100th", 100.Ordinal());
        Assert.AreEqual("101st", 101.Ordinal());
        Assert.AreEqual("102nd", 102.Ordinal());
        Assert.AreEqual("103rd", 103.Ordinal());
        Assert.AreEqual("104th", 104.Ordinal());
        Assert.AreEqual("110th", 110.Ordinal());
        Assert.AreEqual("111th", 111.Ordinal());
        Assert.AreEqual("112th", 112.Ordinal());
        Assert.AreEqual("113th", 113.Ordinal());
        Assert.AreEqual("114th", 114.Ordinal());
        Assert.AreEqual("120th", 120.Ordinal());
        Assert.AreEqual("121st", 121.Ordinal());
        Assert.AreEqual("122nd", 122.Ordinal());
        Assert.AreEqual("123rd", 123.Ordinal());
        Assert.AreEqual("124th", 124.Ordinal());
    }

λ‹΅λ³€

κ°„λ‹¨ν•˜κ³  κΉ¨λ—ν•˜λ©° λΉ λ¦…λ‹ˆλ‹€.

    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();

λ‹΅λ³€

당신은 μžμ‹ μ˜ λ‘€μ„ν•΄μ•Όν•©λ‹ˆλ‹€. λ‚΄ 머리 κΌ­λŒ€κΈ°μ—μ„œ :

public static string Ordinal(this int number)
{
  var work = number.ToString();
  if ((number % 100) == 11 || (number % 100) == 12 || (number % 100) == 13)
    return work + "th";
  switch (number % 10)
  {
    case 1: work += "st"; break;
    case 2: work += "nd"; break;
    case 3: work += "rd"; break;
    default: work += "th"; break;
  }
  return work;
}

그런 λ‹€μŒ ν•  수 μžˆμŠ΅λ‹ˆλ‹€

Console.WriteLine(432.Ordinal());

11/12/13 μ˜ˆμ™Έμ— λŒ€ν•΄ νŽΈμ§‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” λ‚΄ 머리 κΌ­λŒ€κΈ°μ—μ„œ λ§ν–ˆλ‹€ πŸ™‚

1011둜 νŽΈμ§‘ 됨-λ‹€λ₯Έ μ‚¬λžŒλ“€μ€ 이미이 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€. λ‹€λ₯Έ μ‚¬λžŒλ“€ 이이 잘λͺ»λœ 버전을 μž‘μ§€ μ•Šλ„λ‘ν•˜μ‹­μ‹œμ˜€.


λ‹΅λ³€

λ‚˜λŠ” Stu 와 samjudson 의 μ†”λ£¨μ…˜ λͺ¨λ‘μ—μ„œ μš”μ†Œλ₯Ό μ’‹μ•„ ν–ˆμœΌλ©° μ‚¬μš© κ°€λŠ₯ν•œ 콀보라고 μƒκ°ν•˜λŠ” μš”μ†Œλ‘œ ν•¨κ»˜ μž‘λ™ν–ˆμŠ΅λ‹ˆλ‹€.

    public static string Ordinal(this int number)
    {
        const string TH = "th";
        var s = number.ToString();

        number %= 100;

        if ((number >= 11) && (number <= 13))
        {
            return s + TH;
        }

        switch (number % 10)
        {
            case 1:
                return s + "st";
            case 2:
                return s + "nd";
            case 3:
                return s + "rd";
            default:
                return s + TH;
        }
    }

λ‹΅λ³€

아직 λ²€μΉ˜λ§ˆν‚Ήν•˜μ§€λŠ” μ•Šμ•˜μ§€λ§Œ λͺ¨λ“  쑰건뢀 사둀λ₯Ό ν”Όν•¨μœΌλ‘œμ¨ 더 λ‚˜μ€ μ„±λŠ₯을 얻을 수 μžˆμ–΄μ•Όν•©λ‹ˆλ‹€.

이것은 μžλ°”μ΄μ§€λ§Œ C #으둜의 ν¬νŠΈλŠ” κ°„λ‹¨ν•©λ‹ˆλ‹€.

public class NumberUtil {
  final static String[] ORDINAL_SUFFIXES = {
    "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
  };

  public static String ordinalSuffix(int value) {
    int n = Math.abs(value);
    int lastTwoDigits = n % 100;
    int lastDigit = n % 10;
    int index = (lastTwoDigits >= 11 && lastTwoDigits <= 13) ? 0 : lastDigit;
    return ORDINAL_SUFFIXES[index];
  }

  public static String toOrdinal(int n) {
    return new StringBuffer().append(n).append(ordinalSuffix(n)).toString();
  }
}

μ—„κ²©ν•œ λ£¨ν”„μ—μ„œ λ§Žμ€ μ„œμˆ˜λ₯Ό μƒμ„±ν•˜λŠ” 경우 쑰건뢀 κ°μ†Œ 및 λ°°μ—΄ 쑰회λ₯Ό μ‚¬μš©ν•˜λ©΄ μ„±λŠ₯이 ν–₯μƒλ©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ λ‚˜λŠ” 이것이 case statement μ†”λ£¨μ…˜λ§ŒνΌ 읽을 수 μ—†λ‹€λŠ” 것을 μΈμ •ν•©λ‹ˆλ‹€.


이 글은 C# μΉ΄ν…Œκ³ λ¦¬λ‘œ λΆ„λ₯˜λ˜μ—ˆκ³  λ‹˜μ— μ˜ν•΄ 에 μž‘μ„±λμŠ΅λ‹ˆλ‹€.