Convert String to currency format Last Updated : 12 Feb, 2025 Comments Improve Suggest changes Like Article Like Report Given a number N. Convert N into an Indian currency format. Rules of Indian Currency Format are:The last three digits (rightmost) remain grouped together.Beyond the last three digits, groups of two digits are separated by commas.Examples:Input: N = 1000000Output: Rs 10, 00, 000Explanation: Start from the right: 000 (last three digits remain as is).The remaining digits are grouped in twos: 10,00Final format: 10,00,000Input: N = 1500Output: Rs 1,500Explanation: Last three digits remain as is: 500The remaining digit forms a single group: 1,500Final format: 1,500Approach:Steps involved in the implementation of code:We need to check whether the length of the string is even or odd.If the length of the string is less than equal to 3 we will simply return it else we will do the following, newString = "".If the length is even:we will add the first character into a new string newSstring = N[0].and then we will add comma to the new string = ", ".Now we will skip the two characters and then add ", " till the length < (n-2).And at last, we will add the remaining characters to the newString. C++ // C++ implementation of the code #include <bits/stdc++.h> using namespace std; // Function to convert N // into indian currency string goodFormat(string s, int n) { // If length if less than 3 if (n <= 3) return "Rs. " + s; string ans = ""; int start = 0, cnt = 0; // If length is even if (n % 2 == 0) { ans += s[0]; ans += ", "; start = 1; } while (start < n - 2) { if (cnt == 2) { ans += ", "; cnt = 0; continue; } else { ans += s[start]; cnt++; } start++; } for (int i = start; i < n; i++) ans += s[i]; return "Rs " + ans; } // Drivers code int main() { string s = "1000000"; int l = s.length(); // Function Call cout << goodFormat(s, l); return 0; } Java // Java implementation of the code import java.util.*; class GFG { // Function to convert N // into Indian currency static String goodFormat(String s, int n) { // If length is less than 3 if (n <= 3) return "Rs. " + s; String ans = ""; int start = 0, cnt = 0; // If length is even if (n % 2 == 0) { ans += s.charAt(0); ans += ", "; start = 1; } while (start < n - 2) { if (cnt == 2) { ans += ", "; cnt = 0; continue; } else { ans += s.charAt(start); cnt++; } start++; } for (int i = start; i < n; i++) ans += s.charAt(i); return "Rs " + ans; } // Drivers code public static void main(String[] args) { String s = "1000000"; int l = s.length(); // Function Call System.out.println(goodFormat(s, l)); } } // This code is contributed by prasad264 Python # Python3 implementation of the code # Function to convert N # into indian currency def goodFormat(s, n): # If length if less than 3 if (n <= 3): return "Rs. " + s ans = "" start = 0 cnt = 0 # If length is even if (n % 2 == 0): ans += s[0] ans += ", " start = 1 while (start < n - 2): if (cnt == 2): ans += ", " cnt = 0 continue else: ans += s[start] cnt += 1 start += 1 for i in range(start, n): ans += s[i] return "Rs " + ans # Drivers code s = "1000000" l = len(s) # Function Call print(goodFormat(s, l)) C# using System; public class IndianCurrencyFormatter { public static string GoodFormat(string s) { int n = s.Length; // If length if less than 3 if (n <= 3) return "Rs. " + s; string ans = ""; int start = 0, cnt = 0; // If length is even if (n % 2 == 0) { ans += s[0]; ans += ", "; start = 1; } while (start < n - 2) { if (cnt == 2) { ans += ", "; cnt = 0; continue; } else { ans += s[start]; cnt++; } start++; } for (int i = start; i < n; i++) ans += s[i]; return "Rs " + ans; } public static void Main() { string s = "1000000"; // Function Call Console.WriteLine(GoodFormat(s)); } } JavaScript // Javascript implementation of the code // Function to convert N // into indian currency function goodFormat(s, n) { // If length if less than 3 if (n <= 3) return "Rs. " + s; let ans = ""; let start = 0, cnt = 0; // If length is even if (n % 2 == 0) { ans += s[0]; ans += ", "; start = 1; } while (start < n - 2) { if (cnt == 2) { ans += ", "; cnt = 0; continue; } else { ans += s[start]; cnt++; } start++; } for (let i = start; i < n; i++) ans += s[i]; return "Rs " + ans; } // Drivers code let s = "1000000"; let l = s.length; // Function Call console.log(goodFormat(s, l)); OutputRs 10, 00, 000Time Complexity: O(n), as we iterate through the string once, processing each character.Auxiliary Space: O(n), as we store the formatted output in a new string. Comment More infoAdvertise with us Next Article Convert String to currency format S sunny029sharma Follow Improve Article Tags : Strings DSA cpp-string Practice Tags : Strings Similar Reads How to format numbers as currency strings in Python Formatting numbers as currency strings in Python is a common requirement especially in the applications involving financial data. Formatting numbers as currency strings in Python involves presenting numeric values in the standardized currency format typically including the currency symbol, thousands 3 min read Convert the number from International system to Indian system Given string str which represents a number with separators(,) in the International number system, the task is to convert this string representation into the Indian Numeric System. Examples: Input: str = "123,456,789" Output: 12,34,56,789 Explanation: The given string represents a number in the inter 13 min read Validating Indian currency data using Regular expressions Given some Indian Currency Data, the task is to check if they are valid or not using regular expressions. Rules for valid Indian Currency Data are: Indian Rupee format: The currency string starts with the Indian Rupee symbol â¹, followed by a comma-separated integer part that can have one to three di 5 min read PHP money_format() Function The money_format() function is an inbuilt function in PHP that returns a number formatted as a currency string. In the main string, the formatted number is inserted where there is a percentage sign (%). The function is only defined in systems with strfmon() capacities. For example, money_format() is 4 min read How to format numbers as currency string in JavaScript ? A number, represented as monetary value, creates an impact and becomes much more readable, and that's the reason behind formatting a number as currency. For example, a number, let's say 100000 when represented as $100,000.00 it becomes pretty much understood that it represents a monetary value, and 3 min read DecimalFormat getCurrency() method in Java The getCurrency() method is a built-in method of the java.text.DecimalFomrat class in Java and is used to return the currency which is used while formatting currency values by this currency. It can be null if there is no valid currency to be determined or if no currency has been set previously. Synt 2 min read FormatCurrency() and FormatDateTime() Function in MS Access 1. FormatCurrency() Function : FormatCurrency() Function in MS Access is used to Returns an expression formatted as a currency value using the currency symbol defined in the system control panel. Syntax : FormatCurrency ( Expression [, NumDigitsAfterDecimal ] [, IncludeLeadingDigit ] [, UseParensFor 2 min read NumberFormat getCurrency() method in Java with Examples The getCurrency() method is a built-in method of the java.text.NumberFormat returns the currency which is used while formatting currency values by this currency. It can be null if there is no valid currency to be determined or if no currency has been set previously. Syntax: public Currency getCurren 2 min read Angular forms formatCurrency Directive In this article, we are going to see what is formatCurrency in Angular 10 and how to use it. The formatCurrency is used to format a number as currency using locale rules. Syntax: formatCurrency(value, locale, currency, currencyCode, digitsInfo) Parameters: value: The number to format.locale: A local 2 min read JavaScript Number toLocaleString() Method The toLocaleString() method converts a number into a string, using a local language format. The language depends on the locale setup on your computer. Syntax: number.toLocaleString(locales, options) Parameters: This method accepts two parameters locales and options. locales: This is an optional para 2 min read Like