Friday, January 10, 2025
Google search engine
HomeLanguagesJavaCurrency getSymbol Method in Java with Examples

Currency getSymbol Method in Java with Examples

The getSymbol() Method of Currency class in Java is used to retrieve the official symbol of the currency code.

Syntax:

CURRENCY.getSymbol()

Parameters: This method does not accept any parameters.

Return Value: This method returns the official symbol of the currency.

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

Below programs illustrate the working of getSymbol() method:

Program 1:




// Java Code to illustrate getSymbol() 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 symbol of the currency
        String currency_symbol
            = curr_ency.getSymbol();
        System.out.println("Symbol for the currency of India is: "
                           + currency_symbol);
    }
}


Output:

Symbol for the currency of India is: INR

Program 2:




// Java Code to illustrate getSymbol() 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 symbol of the currency
        String currency_symbol
            = curr_ency.getSymbol();
        System.out.println("Symbol for the currency of USA is: "
                           + currency_symbol);
    }
}


Output:

Symbol for the currency of USA is: $

Program 3: For an invalid Currency Code.




// Java Code to illustrate getSymbol() 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 symbol of the currency
            String currency_symbol
                = curr_ency.getSymbol();
            System.out.println("Symbol for the currency of USA is: "
                               + currency_symbol);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.IllegalArgumentException
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments