java 方法 示例_Java货币getSymbol()方法与示例

本文介绍了Java中的`Currency`类的`getSymbol()`方法,用于获取货币的符号。该方法存在于`java.util`包中,提供不带参数和带有`Locale`参数的两个版本。非静态方法需要通过类对象调用,调用时传入`Locale`参数可指定特定语言环境的货币符号。如果`Locale`参数为`null`,则可能抛出`NullPointerException`。示例代码展示了如何使用这两个方法并给出运行结果。
摘要由CSDN通过智能技术生成

java 方法 示例

货币类getSymbol()方法 (Currency Class getSymbol() method)

Syntax:

句法:

    public String getSymbol();
    public String getSymbol(Locale lo);

  • getSymbol() method is available in java.util package.

    getSymbol()方法在java.util包中可用。

  • getSymbol() method is used to get the symbol of this Currency for the default locale.

    getSymbol()方法用于获取默认语言环境的货币符号。

  • getSymbol(Locale lo) method is used to get the symbol of this Currency for the given Locale (lo).

    getSymbol(Locale lo)方法用于获取给定Locale(lo)的此货币的符号。

  • getSymbol() method does not throw an exception at the time of returning symbol.

    getSymbol()方法在返回符号时不会引发异常。

  • getSymbol(Locale lo) method may throw an exception at the time of returning symbol.

    getSymbol(Locale lo)方法在返回符号时可能会引发异常。

    NullPointerException: This exception may throw when the given parameter Locale (lo) is null exists.

    NullPointerException :如果给定参数Locale(lo)为null,则可能引发此异常。

  • These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问;如果尝试使用类名称访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, getSymbol(),

    在第一种情况下, getSymbol()

    • It does not accept any parameter.
  • In the second case, getSymbol(Locale lo)

    在第二种情况下, getSymbol(Locale lo)

    • Locale lo – represent the locale for whose symbol is to be returned.
    • 语言环境lo –表示要为其返回符号的语言环境。

Return value:

返回值:

In both the cases, the return type of the method is String, it gets symbol of this Currency object.

在这两种情况下,方法的返回类型均为String ,它获取此Currency对象的符号。

Example:

例:

// Java program is to demonstrate the example of
// getSymbol() method of Currency

import java.util.*;

public class GetSymbolOfCurrency {
    public static void main(String args[]) {
        // Instantiates a currency with INR code
        // for the default locale
        Currency c1 = Currency.getInstance("INR");

        // Instantiates a currency for the given locale
        Locale lo = Locale.US;
        Currency c2 = Currency.getInstance(lo);

        // By using getSymbol() method is to return
        // the symbol of this Currency for the
        // default locale
        System.out.print("c1.getSymbol(): ");
        System.out.println(c1.getSymbol());

        // By using getSymbol(lo) method is to return
        // the symbol of this Currency for the
        // defined locale

        System.out.print("c2.getSymbol(lo): ");
        System.out.println(c2.getSymbol(lo));
    }
}

Output

输出量

c1.getSymbol(): ₹
c2.getSymbol(lo): $


翻译自: https://www.includehelp.com/java/currency-getsymbol-method-with-example.aspx

java 方法 示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值