Java Program to Convert a String to a Currency Format Last Updated : 07 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The NumberFormat and DecimalFromat class in Java offers a method for formatting numbers based on a location. We can define the formatting cultural conventions, such as the grouping separator, decimal separator, and currency symbol, using the locale parameter. In Java, the NumberFormat class is used for currency formatting. To format numbers as currency, we need to follow these steps: Create an Instance: Use the NumberFormat.getCurrencyInstance() method to obtain a currency formatter instance.Set Locale: We can set the locale using locale to format the currency according to a specific region's conventions.Format Numbers: Use the format() method to format the numerical values as currency. This method returns a formatted string representation of the provided number.Program to Convert a String to a Currency Format in JavaThere are certain approaches to convert string to a currency format in Java mentioned below: Method 1: Using NumberFormat Java // Java program to convert a String to a currency format using number format import java.util.*; import java.math.BigDecimal; import java.text.NumberFormat; public class Main { public static void main(String[] args) { // String amount String str = "10000.58"; // Get a Currency instance Currency inr = Currency.getInstance("INR"); Currency usd = Currency.getInstance("USD"); Currency eur = Currency.getInstance("EUR"); // Local instance for India Locale loc = new Locale("hi", "IN"); // Create a NumberFormatter with custom currency NumberFormat inrFormatter = NumberFormat.getCurrencyInstance(loc); inrFormatter.setCurrency(inr); NumberFormat usdFormatter = NumberFormat.getCurrencyInstance(Locale.US); usdFormatter.setCurrency(usd); NumberFormat eurFormatter = NumberFormat.getCurrencyInstance(Locale.UK); eurFormatter.setCurrency(eur); // Convert string to BigDecimal and format as // currency BigDecimal amt = new BigDecimal(str); String currInr = inrFormatter.format(amt); String currUs = usdFormatter.format(amt); String currUk = eurFormatter.format(amt); System.out.println("INR Currency: " + currInr); System.out.println("USD Currency: " + currUs); System.out.println("EUR Currency: " + currUk); } } OutputINR Currency: ₹10,000.58 USD Currency: $10,000.58 EUR Currency: €10,000.58 Explanation of the Program:In the above program, we have a string str representing the amount "10000.58".We get instances of Currency for INR, USD, and EUR.We create NumberFormat instances for each currency with the respective locales and set the currency using setCurrency().We convert the string str to a BigDecimal object named amt.We use the format() method of each NumberFormat instance to format the BigDecimal amount as a currency string for each currency.Finally, we print the formatted currency strings to the console.Method 2: Using DecimalFormat Java // Java program to convert a String to a currency format using decimal format import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.*; public class GFG { public static void main(String[] args) { // String amount String str = "10000.58"; // Create a DecimalFormat object DecimalFormat formatter = new DecimalFormat("$#,##0.00"); // Convert string to BigDecimal and format as // currency BigDecimal amt = new BigDecimal(str); String currStr = formatter.format(amt); System.out.println("Currency: " + currStr); } } OutputCurrency: $10,000.58 Explanation of the Program:In the above program, we have a string str representing the amount "10000.58".We have created a DecimalFormat object named formatter with the pattern "$#,##0.00", which specifies the currency format with two decimal places and commas for thousands of separators.We have converted the string str to a BigDecimal object named amt.We have used the format() method of the DecimalFormat object to format the BigDecimal amount as a currency string and store it in currStr.Finally, we print the formatted currency string to the console. Comment More infoAdvertise with us Next Article Java Program to Convert Long to String M mrstax Follow Improve Article Tags : Java Java Programs Java Concurrency Java Examples Practice Tags : Java Similar Reads Java Program to Convert a Float value to String Given a Float value in Java, the task is to write a Java program to convert this float value to string type. Examples: Input: 1.0 Output: "1.0" Input: 3.14 Output: "3.14" Strings - Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are 4 min read Java Program to Convert String to Float Value Given a string âstrâ in Java, the task is to convert this string to float type. Methods: This can be done by two methods as listed below: Using parseFloat() Method Using valueOf() Method Let us discuss above two ways of implementing the methods to get a fair understanding of the conversion of string 4 min read Java Program to Convert Double to String The primary goal of double to String conversion in Java is to store big streams of numbers that are coming where even data types fail to store the stream of numbers. It is generically carried out when we want to display the bigger values. In this article, we will learn How to Convert double to Strin 3 min read Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a 4 min read Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s 4 min read How to Convert a String to a LocalDate in Java? Converting a String to a LocalDate in Java is a common operation when you are dealing with date inputs from users. Java provides the LocalDate class in the java.time package for representing a date without time information. LocalDate class is part of the java.time package introduced in Java 8. Strin 2 min read Like