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 μ루μ λ§νΌ μ½μ μ μλ€λ κ²μ μΈμ ν©λλ€.