Program to Convert a String to a Currency Format in Java
There 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); } } |
Output
INR 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 usingsetCurrency()
. - We convert the string
str
to aBigDecimal
object namedamt
. - We use the
format()
method of eachNumberFormat
instance to format theBigDecimal
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); } } |
Output
Currency: $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 namedformatter
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 aBigDecimal
object namedamt
. - We have used the
format()
method of theDecimalFormat
object to format theBigDecimal
amount as a currency string and store it incurrStr
. - Finally, we print the formatted currency string to the console.
Java Program to Convert a String to a Currency Format
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.