Java Currency getSymbol方法及实例

Java Currency getSymbol方法及实例

Java中货币类的 getSymbol() 方法是用来检索货币代码的官方符号。

语法

CURRENCY.getSymbol()

参数: 该方法不接受任何参数。

返回值 :该方法返回货币的官方符号。

异常: 如果调用了一个无效的代码,该方法会抛出运行时错误。

下面的程序说明了getSymbol()方法的工作。

程序 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);
    }
}

输出:

Symbol for the currency of India is: INR

程序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);
    }
}

输出:

Symbol for the currency of USA is: $

程序3: 对于一个无效的货币代码。

// 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);
        }
    }
}

输出:

java.lang.IllegalArgumentException

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程