Java中的toString()貨幣類方法用於檢索該貨幣的貨幣代碼,該貨幣代碼實際上是字符串格式的官方ISO 4217貨幣代碼
用法:
CURRENCY.toString()
參數:此方法不接受任何參數。
返回值:此方法返回貨幣的ISO 4217貨幣代碼。
異常:如果調用了無效的代碼,該方法將引發運行時錯誤。
以下示例程序旨在說明toString()方法的用法:
示例1:
// 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);
}
}
輸出:
Currency Code of India is: INR
示例2:
// 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);
}
}
輸出:
Currency Code of USA is: USD
示例3:輸入無效的貨幣代碼。
// 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);
}
}
}
輸出:
java.lang.IllegalArgumentException
相關用法
- Java Currency getSymbol用法及代碼示例
- Java Currency getInstance()用法及代碼示例
- Java Currency getCurrencyCode()用法及代碼示例
- Java Currency getDefaultFractionDigits()用法及代碼示例
- Java Instant toString()用法及代碼示例
- Java LinkedBlockingQueue toString()用法及代碼示例
- Java AtomicReferenceArray toString()用法及代碼示例
- Java DigestInputStream.toString()用法及代碼示例
- Java DigestOutputStream.toString()用法及代碼示例
- Java Level toString()用法及代碼示例
- Java AtomicIntegerArray toString()用法及代碼示例
- Java LinkedTransferQueue toString()用法及代碼示例
- Java AtomicBoolean toString()用法及代碼示例
- Java Locale toString()用法及代碼示例
- Java AlgorithmParameters toString()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Currency toString() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。