Currency toString() Method in Java with Examples

The toString() Method of Currency class in Java is used to retrieve the currency code of this currency which is actually the official ISO 4217 currency code in a string format

Syntax:

CURRENCY.toString()

Parameters: This method does not accept any parameters.

Return Value: This method returns the ISO 4217 currency code of the currency.

Exceptions: The method throws Runtime Error if an invalid code is called.



Below program illustrates the working of toString() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate toString() method
  
import java.util.*;
  
public class Currency_Demo {
    public static void main(String[] args)
    {
  
        // Creating a currency with the code
        Currency curr_ency
            = Currency.getInstance("INR");
  
        // Getting the currency code
        String currency_code
            = curr_ency.toString();
        System.out.println("Currency Code of India is: "
                           + currency_code);
    }
}

chevron_right


Output:

Currency Code of India is: INR

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate toString() method
  
import java.util.*;
  
public class Currency_Demo {
    public static void main(String[] args)
    {
  
        // Creating a currency with the code
        Currency curr_ency
            = Currency.getInstance("USD");
  
        // Getting the currency code
        String currency_code
            = curr_ency.toString();
        System.out.println("Currency Code of USA is: "
                           + currency_code);
    }
}

chevron_right


Output:

Currency Code of USA is: USD

Program 3: For an invalid Currency Code.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate toString() method
import java.util.*;
  
public class Currency_Demo {
    public static void main(String[] args)
    {
        try {
  
            // Creating a currency with the code
            Currency curr_ency = Currency.getInstance("USDA");
  
            // Getting the currency code
            String currency_code = curr_ency.toString();
  
            System.out.println("Invalid Currency Code: "
                               + currency_code);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.lang.IllegalArgumentException

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.