Created
January 24, 2024 16:21
-
-
Save ruturajpatki/092ea2a8f407df442bbe2086c4b20884 to your computer and use it in GitHub Desktop.
Pure PHP implementation to format any given number in Indian Currency Format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function formatIndianCurrency($number) { | |
| // Step 0: Check if the number is negative | |
| $isNegativeNumber = false; | |
| if ($number < 0) { | |
| $isNegativeNumber = true; | |
| $number = abs($number); | |
| } | |
| // Step 1: Check for decimal point | |
| if (strpos($number, '.') !== false) { | |
| list($wholeNumber, $decimalNumber) = explode('.', $number); | |
| } else { | |
| $wholeNumber = $number; | |
| $decimalNumber = ''; | |
| } | |
| // Step 2: Check length of wholeNumber | |
| if (strlen($wholeNumber) < 3) { | |
| $result = $wholeNumber . ($decimalNumber ? '.' . $decimalNumber : ''); | |
| } else { | |
| // Step 3: Split into hundredsNumber and otherNumber | |
| $hundredsNumber = substr($wholeNumber, -3); | |
| $otherNumber = substr($wholeNumber, 0, -3); | |
| // Step 4: Process otherNumber in reverse | |
| $otherNumberArray = str_split(strrev($otherNumber)); | |
| $finalOtherNumber = ''; | |
| foreach ($otherNumberArray as $index => $char) { | |
| $finalOtherNumber = $char . $finalOtherNumber; | |
| if ($index % 2 == 1 && $index < count($otherNumberArray) - 1) { | |
| $finalOtherNumber = ',' . $finalOtherNumber; | |
| } | |
| } | |
| // Step 5: Concatenate and return the result | |
| $result = '₹' . (($finalOtherNumber == '') ? '' : $finalOtherNumber . ',') . $hundredsNumber . ($decimalNumber ? '.' . $decimalNumber : ''); | |
| } | |
| // Check if the number was originally negative | |
| if ($isNegativeNumber) { | |
| $result = '-' . $result; | |
| } | |
| return $result; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Formatting Numbers into Indian Currency Format using Python
We will write a Python function to format numbers into the Indian currency format. Indian currency formatting is unique as it uses commas differently compared to other numbering systems. For instance, the number 123456789 is formatted as 12,34,56,789 in Indian currency.
Step 1: Converting the Number to a String
First, we need to convert the input number to a string. This allows us to easily manipulate and format the number.
This initial step ensures that we are working with a string representation of the number, making it easier to handle subsequent formatting operations.
Step 2: Splitting the Number into Integer and Decimal Parts
Next, we need to check if the number has a decimal point. If it does, we split the number into integer and decimal parts. If not, we just keep the integer part.
This step helps us manage numbers with and without decimal points separately, ensuring accurate formatting for both types.
Step 3: Handling the Integer Part
We then convert the integer part into a character array for easy manipulation.
If the length of the integer part is less than or equal to 3, we don't need to format it further.
This conditional check allows us to bypass unnecessary formatting for shorter numbers.
Step 4: Formatting the Integer Part
For numbers longer than 3 digits, we first get the last 3 digits and then process the remaining digits.
We then traverse the remaining digits in reverse order, adding a comma after every 2 digits.
This loop ensures that commas are inserted in the correct positions according to the Indian numbering system.
Step 5: Adding the Decimal Part
Finally, if the number has a decimal part, we append it to the formatted number.
This step ensures that any fractional part of the number is preserved in the final formatted output.
Step 6: Returning the Formatted Number
The function then returns the formatted number.
Here is the complete function:
Testing the Function
Let’s test the function with some examples to see how it works.