Java中货币格式化 private final static NumberFormat CURRENCY_FORMAT = NumberFormat.getCurrencyInstance(Locale.CHINA); //建立货币格式化引用 public static String moneyCurrencyFormat(String money) { if (!TextUtils.isEmpty(money)) { money = CURRENCY_FORMAT.format(new BigDecimal(money)); } return money; }...
importjava.text.NumberFormat;importjava.util.Currency;publicclassMain{publicstaticvoidmain(String[]args){// 实例化 NumberFormat 对象NumberFormatcurrencyFormat=NumberFormat.getCurrencyInstance();// 设置货币格式currencyFormat.setCurrency(Currency.getInstance("USD"));// 使用 format 方法格式化货币StringformattedCu...
importjava.text.DecimalFormat;publicclassCurrencyFormatExample{publicstaticvoidmain(String[]args){doubleamount=12345.6789;DecimalFormatcurrencyFormat=newDecimalFormat("#,##0.00");StringformattedAmount=currencyFormat.format(amount);System.out.println(formattedAmount);}} 1. 2. 3. 4. 5. 6. 7. 8. 9. ...
private static final String CURRENCY_FORMAT_OVERRIDE = "\"$\"#,##0.00_);-\"$\"#,##0.00"; private static final String CURRENCY_FORMAT_TARGET = "\"$\"#,##0.00_);(\"$\"#,##0.00)"; static { // static class level initializer Field field = org.apache.poi.ss.usermodel.BuiltinForm...
where formattedPayment is payment formatted according to the appropriateLocale's currency. Note:India does not have a built-in Locale, so you mustconstruct onewhere the language isen(i.e., English). Input Format A single double-precision number denoting . ...
out.println("中国语言环境的货币格式: " + chinaCurrencyFormat.format(amount)); } } 复制代码 运行上述代码,将会看到如下输出: 默认语言环境的货币格式: ¥123,456,789.12 美国语言环境的货币格式: $123,456,789.12 中国语言环境的货币格式: ¥123,456,789.12 复制代码 请注意,默认语言环境和中国语言环境的...
*/publicstaticStringformatCurrency(Objectobj){if(obj==null){return"";}// format方法不能识别字符串类型if(objinstanceofString){obj=newBigDecimal(String.valueOf(obj));}NumberFormatcurrencyInstance=NumberFormat.getCurrencyInstance(Locale.CHINA);returncurrencyInstance.format(obj);}/** ...
public static String formatCurrency(String amount) { DecimalFormat formatter = new DecimalFormat("###,###,##0.00"); return formatter.format(Double.parseDouble(amount)); } 100 -> "100.00" 100.1 -> "100.10" Share Improve this answer Follow edited Oct 17, 2017 at 14:56 answered Oct 17...
String str = number.format(format);//12,343,171.6 //获取整数数值格式 NumberFormat integer = NumberFormat.getIntegerInstance(); str = integer.format(format);//如果带小数会四舍五入到整数12,343,172 //获取货币数值格式 NumberFormat currency = NumberFormat.getCurrencyInstance(); ...
首先,要特别注意的是 NumberFormat和DecimalFormat是线程不安全的。 这意味你如果同时有多个线程操作一个format实例对象,会出现意想不到的结果。 解决方法有两个: 为每个线程都创建一个 format实例,通过ThreadLocal 来给每个线程都产生一个本地实例。如果你对ThreadLocal不了解,可以看我这篇文章:ThreadLocal 的用法以及...