NumberFormat setCurrency() method in Java with Examples
The setCurrency() method is a built-in method of the java.text.NumberFormat which sets the currency used by this number format when formatting currency values. This does not update the minimum or maximum number of fraction digits used by the number format. It overwrites the initially currency.
public void setCurrency(Currency currency)
Parameters: The function accepts a mandatory parameter currency which specifies the currency to be set up.
Return Value: The function returns nothing, hence has a return type void.
Errors and Exceptions: The function throws two types of exceptions which can be described as below:
- UnsupportedOperationException: it is thrown if the number format class doesn’t implement currency formatting
- NullPointerException it is thrown if currency is null
Below is the implementation of the above function:
Initially Currency: USD Currency set as: CAD
Initially Currency: USD Exception is: java.lang.NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.