25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_fmt_money.py 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. import webnotes
  4. from webnotes import _
  5. from webnotes.utils import flt, cstr
  6. def fmt_money(amount, precision=None):
  7. """
  8. Convert to string with commas for thousands, millions etc
  9. """
  10. number_format = webnotes.conn.get_default("number_format") or "#,###.##"
  11. decimal_str, comma_str, precision = get_number_format_info(number_format)
  12. amount = '%.*f' % (precision, flt(amount))
  13. if amount.find('.') == -1:
  14. decimals = ''
  15. else:
  16. decimals = amount.split('.')[1]
  17. parts = []
  18. minus = ''
  19. if flt(amount) < 0:
  20. minus = '-'
  21. amount = cstr(abs(flt(amount))).split('.')[0]
  22. if len(amount) > 3:
  23. parts.append(amount[-3:])
  24. amount = amount[:-3]
  25. val = number_format=="#,##,###.##" and 2 or 3
  26. while len(amount) > val:
  27. parts.append(amount[-val:])
  28. amount = amount[:-val]
  29. parts.append(amount)
  30. parts.reverse()
  31. amount = comma_str.join(parts) + (precision and (decimal_str + decimals) or "")
  32. amount = minus + amount
  33. return amount
  34. def get_number_format_info(format):
  35. if format=="#.###":
  36. return "", ".", 0
  37. elif format=="#,###":
  38. return "", ",", 0
  39. elif format=="#,###.##" or format=="#,##,###.##":
  40. return ".", ",", 2
  41. elif format=="#.###,##":
  42. return ",", ".", 2
  43. elif format=="# ###.##":
  44. return ".", " ", 2
  45. else:
  46. return ".", ",", 2
  47. import unittest
  48. class TestFmtMoney(unittest.TestCase):
  49. def test_standard(self):
  50. webnotes.conn.set_default("number_format", "#,###.##")
  51. self.assertEquals(fmt_money(100), "100.00")
  52. self.assertEquals(fmt_money(1000), "1,000.00")
  53. self.assertEquals(fmt_money(10000), "10,000.00")
  54. self.assertEquals(fmt_money(100000), "100,000.00")
  55. self.assertEquals(fmt_money(1000000), "1,000,000.00")
  56. self.assertEquals(fmt_money(10000000), "10,000,000.00")
  57. self.assertEquals(fmt_money(100000000), "100,000,000.00")
  58. self.assertEquals(fmt_money(1000000000), "1,000,000,000.00")
  59. def test_negative(self):
  60. webnotes.conn.set_default("number_format", "#,###.##")
  61. self.assertEquals(fmt_money(-100), "-100.00")
  62. self.assertEquals(fmt_money(-1000), "-1,000.00")
  63. self.assertEquals(fmt_money(-10000), "-10,000.00")
  64. self.assertEquals(fmt_money(-100000), "-100,000.00")
  65. self.assertEquals(fmt_money(-1000000), "-1,000,000.00")
  66. self.assertEquals(fmt_money(-10000000), "-10,000,000.00")
  67. self.assertEquals(fmt_money(-100000000), "-100,000,000.00")
  68. self.assertEquals(fmt_money(-1000000000), "-1,000,000,000.00")
  69. def test_decimal(self):
  70. webnotes.conn.set_default("number_format", "#.###,##")
  71. self.assertEquals(fmt_money(-100), "-100,00")
  72. self.assertEquals(fmt_money(-1000), "-1.000,00")
  73. self.assertEquals(fmt_money(-10000), "-10.000,00")
  74. self.assertEquals(fmt_money(-100000), "-100.000,00")
  75. self.assertEquals(fmt_money(-1000000), "-1.000.000,00")
  76. self.assertEquals(fmt_money(-10000000), "-10.000.000,00")
  77. self.assertEquals(fmt_money(-100000000), "-100.000.000,00")
  78. self.assertEquals(fmt_money(-1000000000), "-1.000.000.000,00")
  79. def test_lacs(self):
  80. webnotes.conn.set_default("number_format", "#,##,###.##")
  81. self.assertEquals(fmt_money(100), "100.00")
  82. self.assertEquals(fmt_money(1000), "1,000.00")
  83. self.assertEquals(fmt_money(10000), "10,000.00")
  84. self.assertEquals(fmt_money(100000), "1,00,000.00")
  85. self.assertEquals(fmt_money(1000000), "10,00,000.00")
  86. self.assertEquals(fmt_money(10000000), "1,00,00,000.00")
  87. self.assertEquals(fmt_money(100000000), "10,00,00,000.00")
  88. self.assertEquals(fmt_money(1000000000), "1,00,00,00,000.00")
  89. def test_no_precision(self):
  90. webnotes.conn.set_default("number_format", "#,###")
  91. self.assertEquals(fmt_money(0.3), "0")
  92. self.assertEquals(fmt_money(100.3), "100")
  93. self.assertEquals(fmt_money(1000.3), "1,000")
  94. self.assertEquals(fmt_money(10000.3), "10,000")
  95. self.assertEquals(fmt_money(-0.3), "0")
  96. self.assertEquals(fmt_money(-100.3), "-100")
  97. self.assertEquals(fmt_money(-1000.3), "-1,000")
  98. if __name__=="__main__":
  99. webnotes.connect()
  100. unittest.main()