Thermometer: show current monthly income in entire euros
authorSybren A. Stüvel <sybren@stuvel.eu>
Tue, 26 Mar 2019 10:56:53 +0000 (11:56 +0100)
committerSybren A. Stüvel <sybren@stuvel.eu>
Tue, 26 Mar 2019 10:56:53 +0000 (11:56 +0100)
looper/money.py
looper/tests/test_money.py
websrc/templates/blender_fund/landing.pug

index ef7707a..28e4a97 100644 (file)
@@ -80,13 +80,19 @@ class Money:
         """Return the amount as string with currency symbol, so '€' instead of 'EUR'."""
         return f'{self.currency_symbol}\u00A0{self.decimals_string}'
 
+    def with_currency_symbol_rounded(self) -> str:
+        """Same as with_currency_symbol() rounding to entire units."""
+
+        whole_rounded = int(round(self._cents / 100))
+        return f'{self.currency_symbol}\u00A0{whole_rounded}'
+
     def with_currency_symbol_nonocents(self) -> str:
         """Same as with_currency_symbols but never ends in '.00'."""
 
-        cents = self.just_cents
+        whole, cents = divmod(self._cents, 100)
         if cents:
-            return self.with_currency_symbol()
-        return f'{self.currency_symbol}\u00A0{self.just_whole}'
+            return f'{self.currency_symbol}\u00A0{whole}.{cents:02}'
+        return f'{self.currency_symbol}\u00A0{whole}'
 
     def __pos__(self) -> 'Money':
         return Money(self._currency, self._cents)
index 66a406c..4ed3596 100644 (file)
@@ -160,6 +160,14 @@ class BasicOperationsTest(TestCase):
         self.assertEqual('$\u00A010.50', USD(1050).with_currency_symbol_nonocents())
         self.assertEqual('¥\u00A00.99', Money('JPY', 99).with_currency_symbol_nonocents())
 
+    def test_with_currency_symbol_rounded(self) -> None:
+        self.assertEqual('€\u00A010', EUR(1000).with_currency_symbol_rounded())
+        self.assertEqual('$\u00A010', USD(1050).with_currency_symbol_rounded())
+        self.assertEqual('$\u00A011', USD(1051).with_currency_symbol_rounded())
+        self.assertEqual('$\u00A011', USD(1149).with_currency_symbol_rounded())
+        self.assertEqual('$\u00A012', USD(1150).with_currency_symbol_rounded())
+        self.assertEqual('¥\u00A01', Money('JPY', 99).with_currency_symbol_rounded())
+
 
 class SumTest(TestCase):
     def test_empty(self) -> None:
index 2812ad6..f21a350 100644 (file)
@@ -35,7 +35,7 @@
                                .transform-flip-h.d-flex
                                        span.d-block.d-md-none
                                                strong Current Income
-                                       | {{ summed_income.with_currency_symbol_nonocents }} / month
+                                       | {{ summed_income.with_currency_symbol_rounded }} / month
 
                        .progress-label.target(title="Next Milestone")
                                | #[strong 5 Developers] #[span {{currency_symbol}}&nbsp;{{thermo_mid}}k / month]