is there a quick and dirty way to add 'st' 'rd' 'th' to numbers?
-
what about 21st, 22nd etc? Regards, Rob Philpott.
-
public string Dirty(int number){
switch (number){
case 1:
return "1st";
case 2:
return "2nd";
case 3:
return "3rd";
default:
return number.ToString() + "th";
}
}Dirty enough?
public string Dirty(int number)
{
if (number <= 0) return "";
switch (number/10 == 1 ? number : number%10)
{
case 1: return number + "st";
case 2: return number + "nd";
case 3: return number + "rd";
default: return number + "th";
}
}:-> xacc.ide-0.1.1 released! :) Download and screenshots -- modified at 4:16 Tuesday 27th December, 2005
-
what about 21st, 22nd etc? Regards, Rob Philpott.
To be honest I was unsure about those dates and just entered "21th" into google. Because I got over 2 mio hits i thought it must be correct. Now I tried and "21st" and I got over 150 mio hits :omg:
-
public string Dirty(int number)
{
if (number <= 0) return "";
switch (number/10 == 1 ? number : number%10)
{
case 1: return number + "st";
case 2: return number + "nd";
case 3: return number + "rd";
default: return number + "th";
}
}:-> xacc.ide-0.1.1 released! :) Download and screenshots -- modified at 4:16 Tuesday 27th December, 2005
11st? 12nd?!! :) Regards, Rob Philpott.
-
11st? 12nd?!! :) Regards, Rob Philpott.
-
well, dirty is fine.. but i wanted it correct too :)
-
is there a quick and dirty way to add 'st' 'rd' 'th' to numbers?
I think that for any number whenever the digit at tenth place is '1' then 'th' is always appended to the number. For all other numbers ending with 1,2,3 digits are appended with 'st','nd','rd' respectively for all other digits 'th' is appended. And this is my code. public string Dirty(int number) { int num=number%100; int tenplace=num/10; if(tenplace==1) { return number + "th"; } switch (number%10) { case 1: return number + "st"; case 2: return number + "nd"; case 3: return number + "rd"; default: return number + "th"; } }
-
I think that for any number whenever the digit at tenth place is '1' then 'th' is always appended to the number. For all other numbers ending with 1,2,3 digits are appended with 'st','nd','rd' respectively for all other digits 'th' is appended. And this is my code. public string Dirty(int number) { int num=number%100; int tenplace=num/10; if(tenplace==1) { return number + "th"; } switch (number%10) { case 1: return number + "st"; case 2: return number + "nd"; case 3: return number + "rd"; default: return number + "th"; } }
-
Very nice, but unless you want it to return -21th, or 0th, you need to add: try { if (number < 1) return ""; //YOUR CODE HERE } catch { return ""; } If you don't add the try/catch and the number is too big you will get an error.
yeh u r right. But I think 0th is valid. instead of number < 1 we should use number < 0 would work better