java 货币 类型,Java中的美元货币格式

"这篇博客介绍了如何在Java中使用NumberFormat类将浮点数和BigDecimal高效地转换为货币格式的字符串,例如将1234.56转换为"$1,234.56"。示例代码展示了如何根据当前locale或指定的locale(如英国)来格式化数字。"
摘要由CSDN通过智能技术生成

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

I'm looking for the following:

String 12345.67 becomes String $12,345.67

I'm also looking to do this with Float and BigDecimal as well.

解决方案

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

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

Another example:

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

This will print: £120.00

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值