- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

- cents := n as string
- if size of cents < 2, then
- return '0.0' concatenate cents

- if size of cents is same as 2, then
- return '0.' concatenate cents

- currency := substring of cents except last two digits
- cents := '.' concatenate last two digit
- while size of currency > 3, do
- cents := ',' concatenate last three digit of currency concatenate cents
- currency := substring of cents except last three digits

- cents := currency concatenate cents
- return cents

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))

523644

5,236.44

- Related Questions & Answers
- Program to find maximum amount of coin we can collect from a given matrix in Python
- Program to find total amount of money we have in bank in Python
- Find a specified amount of records in MongoDB?
- C program to find amount of volume passed through a tunnel
- Program to find how many total amount of rain we can catch in Python
- Program to schedule tasks to take smallest amount of time in Python
- Find amount of water wasted after filling the tank in C++
- Program to Find Out the Amount of Rain to be Caught between the Valleys in C++
- Display the maximum amount of memory in Java
- Find minimum number of currency notes and values that sum to given amount in C++
- Program to find minimum amount needed to be paid all good performers in Python
- Find out the minimum number of coins required to pay total amount in C++
- Find the minimum and maximum amount to buy all N candies in Python
- Program to find how many years it will take to reach t amount in Python
- Display the amount of free memory in the Java Virtual Machine

Advertisements