diff --git a/frappe/tests/test_utils.py b/frappe/tests/test_utils.py index 948e26a74f..9d7690dce4 100644 --- a/frappe/tests/test_utils.py +++ b/frappe/tests/test_utils.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import unittest from frappe.utils import evaluate_filters, money_in_words, scrub_urls, get_url +from frappe.utils import ceil, floor class TestFilters(unittest.TestCase): def test_simple_dict(self): @@ -84,3 +85,22 @@ class TestDataManipulation(unittest.TestCase): self.assertTrue(''.format(url) in html) self.assertTrue('style="background-image: url(\'{0}/assets/frappe/bg.jpg\') !important"'.format(url) in html) self.assertTrue('email' in html) + +class TestMathUtils(unittest.TestCase): + def test_floor(self): + from decimal import Decimal + self.assertEqual(floor(2), 2 ) + self.assertEqual(floor(12.32904), 12) + self.assertEqual(floor(22.7330), 22) + self.assertEqual(floor('24.7'), 24) + self.assertEqual(floor('26.7'), 26) + self.assertEqual(floor(Decimal(29.45)), 29) + + def test_ceil(self): + from decimal import Decimal + self.assertEqual(ceil(2), 2 ) + self.assertEqual(ceil(12.32904), 13) + self.assertEqual(ceil(22.7330), 23) + self.assertEqual(ceil('24.7'), 25) + self.assertEqual(ceil('26.7'), 27) + self.assertEqual(ceil(Decimal(29.45)), 30) diff --git a/frappe/utils/data.py b/frappe/utils/data.py index d6618d3b07..86fef0b61c 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -24,7 +24,7 @@ DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT # datetime functions def getdate(string_date=None): """ - Coverts string date (yyyy-mm-dd) to datetime.date object + Converts string date (yyyy-mm-dd) to datetime.date object """ if not string_date: @@ -208,7 +208,7 @@ def get_user_format(): def formatdate(string_date=None, format_string=None): """ - Convers the given string date to :data:`user_format` + Converts the given string date to :data:`user_format` User format specified in defaults Examples: @@ -282,6 +282,44 @@ def cint(s): except: num = 0 return num +def floor(s): + """ + A number representing the largest integer less than or equal to the specified number + + Parameters + ---------- + s : int or str or Decimal object + The mathematical value to be floored + + Returns + ------- + int + number representing the largest integer less than or equal to the specified number + + """ + try: num = cint(math.floor(flt(s))) + except: num = 0 + return num + +def ceil(s): + """ + The smallest integer greater than or equal to the given number + + Parameters + ---------- + s : int or str or Decimal object + The mathematical value to be ceiled + + Returns + ------- + int + smallest integer greater than or equal to the given number + + """ + try: num = cint(math.ceil(flt(s))) + except: num = 0 + return num + def cstr(s, encoding='utf-8'): return frappe.as_unicode(s, encoding) @@ -436,7 +474,7 @@ def get_number_format_info(format): return number_format_info.get(format) or (".", ",", 2) # -# convet currency to words +# convert currency to words # def money_in_words(number, main_currency = None, fraction_currency=None): """