Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. is there a quick and dirty way to add 'st' 'rd' 'th' to numbers?

is there a quick and dirty way to add 'st' 'rd' 'th' to numbers?

Scheduled Pinned Locked Moved C#
question
15 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rob Philpott

    what about 21st, 22nd etc? Regards, Rob Philpott.

    C Offline
    C Offline
    Colin Angus Mackay
    wrote on last edited by
    #6

    That's too clean - he wanted it dirty, remember. :-D ;P


    My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius

    J 1 Reply Last reply
    0
    • R Robert Rohde

      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?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #7

      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

      R 1 Reply Last reply
      0
      • R Rob Philpott

        what about 21st, 22nd etc? Regards, Rob Philpott.

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #8

        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:

        1 Reply Last reply
        0
        • L leppie

          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

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #9

          11st? 12nd?!! :) Regards, Rob Philpott.

          L 1 Reply Last reply
          0
          • R Rob Philpott

            11st? 12nd?!! :) Regards, Rob Philpott.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #10

            OK ok :) xacc.ide-0.1.1 released! :) Download and screenshots

            C 1 Reply Last reply
            0
            • C Colin Angus Mackay

              That's too clean - he wanted it dirty, remember. :-D ;P


              My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius

              J Offline
              J Offline
              Joshua Lunsford
              wrote on last edited by
              #11

              well, dirty is fine.. but i wanted it correct too :)

              1 Reply Last reply
              0
              • L leppie

                OK ok :) xacc.ide-0.1.1 released! :) Download and screenshots

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #12

                And don't forget 13rd ;P


                My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius

                1 Reply Last reply
                0
                • J Joshua Lunsford

                  is there a quick and dirty way to add 'st' 'rd' 'th' to numbers?

                  M Offline
                  M Offline
                  manasvarpe
                  wrote on last edited by
                  #13

                  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"; } }

                  P 1 Reply Last reply
                  0
                  • M manasvarpe

                    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"; } }

                    P Offline
                    P Offline
                    peshkunta
                    wrote on last edited by
                    #14

                    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.

                    M 1 Reply Last reply
                    0
                    • P peshkunta

                      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.

                      M Offline
                      M Offline
                      manasvarpe
                      wrote on last edited by
                      #15

                      yeh u r right. But I think 0th is valid. instead of number < 1 we should use number < 0 would work better

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups