Java 如何将数字格式从特定货币更改为美元货币

Java 如何将数字格式从特定货币更改为美元货币,java,money-format,Java,Money Format,我有这段代码 双倍价格=rs.getDouble(“价格”) NumberFormat nf=NumberFormat.getCurrencyInstance() 字符串formattedprice=nf.format(price) buff.追加(“(“+格式价格+”) 运行代码时,一切正常,但货币设置为,例如: (C800)。我猜是因为我的国家有“冒号”表示货币,但这是过去的事情,我们现在使用美元货币。因此,我如何更改货币以显示此值:($400)而不是此值(C300)。非常感谢您的帮助。假设

我有这段代码

双倍价格=rs.getDouble(“价格”)

NumberFormat nf=NumberFormat.getCurrencyInstance()

字符串formattedprice=nf.format(price)

buff.追加(“(“+格式价格+”)

运行代码时,一切正常,但货币设置为,例如:
(C800)。我猜是因为我的国家有“冒号”表示货币,但这是过去的事情,我们现在使用美元货币。因此,我如何更改货币以显示此值:($400)而不是此值(C300)。非常感谢您的帮助。

假设您使用的是美元,您可以使用
NumberFormat.getCurrencyInstance(Locale.US)
改用:

NumberFormat nf= NumberFormat.getCurrencyInstance(Locale.US);

您可以将区域设置作为参数添加到getCurrencyInstance:

NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

您可以将其默认为$

这是解决办法

    double price = rs.getDouble("price");
    NumberFormat nf= NumberFormat.getCurrencyInstance();
    nf.setMaximumFractionDigits(2);
        Currency currency = Currency.getInstance(Locale.US);
        nf.setCurrency(currency);
        String formatedPrice = nf.format(price);
         System.out.println("( " + formatedPrice + ")");

这是一个解决方案

非常有效,感谢大家的回答。我不能给所有人打分,因为我是noob。哦,非常感谢你的建议,我不知道
    double price = rs.getDouble("price");
    NumberFormat nf= NumberFormat.getCurrencyInstance();
    nf.setMaximumFractionDigits(2);
        Currency currency = Currency.getInstance(Locale.US);
        nf.setCurrency(currency);
        String formatedPrice = nf.format(price);
         System.out.println("( " + formatedPrice + ")");