Java中的美元货币格式 [英] USD Currency Formatting in Java

查看:498
本文介绍了Java中的美元货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如何有效地将 1234.56 等浮点数和类似的BigDecimals转换为字符串,如 $ 1,234.56

In Java, how can I efficiently convert floats like 1234.56 and similar BigDecimals into Strings like $1,234.56

我正在寻找以下内容:

String 12345.67 变成字符串 $ 12,345.67

String 12345.67 becomes String $12,345.67

我也希望用 Float BigDecimal

推荐答案

有一个区域设置敏感的成语很好:

There's a locale-sensitive idiom that works well:

import java.text.NumberFormat;

// Get a currency formatter for the current locale.
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println(fmt.format(120.00));

如果您当前的区域设置位于美国,则 println 将打印$ 120.00

If your current locale is in the US, the println will print $120.00

另一个例子:

import java.text.NumberFormat;
import java.util.Locale;

Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
System.out.println(fmt.format(120.00));

这将打印:£120.00

This will print: £120.00

这篇关于Java中的美元货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆