Add function to format ordinals
authorMichael Hanselmann <hansmi@google.com>
Tue, 22 Feb 2011 17:58:38 +0000 (18:58 +0100)
committerMichael Hanselmann <hansmi@google.com>
Thu, 24 Feb 2011 13:25:14 +0000 (14:25 +0100)
See [1] for the rules.

[1] http://en.wikipedia.org/wiki/Names_of_numbers_in_English#Ordinal_numbers

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

lib/utils/text.py
test/ganeti.utils.text_unittest.py

index c51d729..e55baaa 100644 (file)
@@ -486,3 +486,29 @@ def BuildShellCmd(template, *args):
       raise errors.ProgrammerError("Shell argument '%s' contains"
                                    " invalid characters" % word)
   return template % args
+
+
+def FormatOrdinal(value):
+  """Formats a number as an ordinal in the English language.
+
+  E.g. the number 1 becomes "1st", 22 becomes "22nd".
+
+  @type value: integer
+  @param value: Number
+  @rtype: string
+
+  """
+  tens = value % 10
+
+  if value > 10 and value < 20:
+    suffix = "th"
+  elif tens == 1:
+    suffix = "st"
+  elif tens == 2:
+    suffix = "nd"
+  elif tens == 3:
+    suffix = "rd"
+  else:
+    suffix = "th"
+
+  return "%s%s" % (value, suffix)
index 2bc0c23..417a001 100755 (executable)
@@ -438,5 +438,21 @@ class TestBuildShellCmd(unittest.TestCase):
     self.assertEqual(utils.BuildShellCmd("ls %s", "ab"), "ls ab")
 
 
+class TestOrdinal(unittest.TestCase):
+  def test(self):
+    checks = {
+      0: "0th", 1: "1st", 2: "2nd", 3: "3rd", 4: "4th", 5: "5th", 6: "6th",
+      7: "7th", 8: "8th", 9: "9th", 10: "10th", 11: "11th", 12: "12th",
+      13: "13th", 14: "14th", 15: "15th", 16: "16th", 17: "17th",
+      18: "18th", 19: "19th", 20: "20th", 21: "21st", 25: "25th", 30: "30th",
+      32: "32nd", 40: "40th", 50: "50th", 55: "55th", 60: "60th", 62: "62nd",
+      70: "70th", 80: "80th", 83: "83rd", 90: "90th", 91: "91st",
+      582: "582nd", 999: "999th",
+      }
+
+    for value, ordinal in checks.items():
+      self.assertEqual(utils.FormatOrdinal(value), ordinal)
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()