Suppose we have a positive number n, where n is representing the amount of cents we have, we have to find the formatted currency amount.
So, if the input is like n = 123456, then the output will be "1,234.56".
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, n): cents = str(n) if len(cents) < 2: return '0.0' + cents if len(cents) == 2: return '0.' + cents currency = cents[:-2] cents = '.' + cents[-2:] while len(currency) > 3: cents = ',' + currency[-3:] + cents currency = currency[:-3] cents = currency + cents return cents ob = Solution() print(ob.solve(523644))