- 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

# Program to find the formatted amount of cents of given amount in Python

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 −

## Example

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

## Input

523644

## Output

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
- C program to find amount of volume passed through a tunnel
- Find a specified amount of records in MongoDB?
- Program to find how many total amount of rain we can catch in Python
- C++ program to find maximum possible amount of allowance after playing the game
- Program to schedule tasks to take smallest amount of time in Python
- Program to Find Out the Amount of Rain to be Caught between the Valleys in C++
- Find amount of water wasted after filling the tank 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++
- C++ code to find out the total amount of sales we made
- C++ Program to find out the least amount of money required to subscribe to the OTT services
- 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++

Advertisements