NumberFormat getCurrencyInstance() method in Java with Examples
Last Updated :
02 Jan, 2023
getCurrencyInstance()
The getCurrencyInstance() method is a built-in method of the java.text.NumberFormat returns a currency format for the current default FORMAT locale. Syntax:
public static final NumberFormat getCurrencyInstance()
- Parameters: The function does not accepts any parameter.
- Return Value: The function returns the NumberFormat instance for currency formatting.
Below is the implementation of the above function:
Program 1:
Java
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
public class Main {
public static void main(String[] args)
throws Exception
{
NumberFormat nF
= NumberFormat
.getCurrencyInstance();
nF.setCurrency(
Currency.getInstance(
Locale.CANADA));
String values
= nF.getCurrency()
.getDisplayName();
int amount = 1078 ;
System.out.println(values);
System.out.println(nF.format(amount));
}
}
|
Output
Canadian Dollar
CA$1,078.00
Program 2:
Java
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
public class Main {
public static void main(String[] args)
throws Exception
{
NumberFormat nF
= NumberFormat
.getCurrencyInstance(Locale.US);
String values
= nF.getCurrency()
.getDisplayName();
double amount = 2193.56 ;
System.out.println(values);
System.out.println(nF.format(amount));
}
}
|
Output
US Dollar
$2,193.56
Reference: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#getCurrencyInstance()
getCurrencyInstance(Locale inLocale)
The getCurrencyInstance(Locale inLocale) method is a built-in method of the java.text.NumberFormat returns a currency format for any specifies locale. Syntax:
public static NumberFormat getCurrencyInstance?(Locale inLocale)
- Parameters: The function accepts a single mandatory parameter inLocale which describes the locale which is to specifies.
- Return Value: The function returns the NumberFormat instance for currency formatting.
Below is the implementation of the above function:
Program 1:
Java
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
public class Main {
public static void main(String[] args)
throws Exception
{
NumberFormat nF
= NumberFormat
.getCurrencyInstance(
Locale.CANADA);
String values
= nF.getCurrency()
.getDisplayName();
double amount = 4538.89 ;
System.out.println(values);
System.out.println(nF.format(amount));
}
}
|
Output
Canadian Dollar
$4,538.89
Reference: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#getCurrencyInstance(java.util.Locale)
Share your thoughts in the comments
Please Login to comment...