当前位置: 首页>>代码示例>>Java>>正文


Java NumberFormat.setCurrency方法代码示例

本文整理汇总了Java中java.text.NumberFormat.setCurrency方法的典型用法代码示例。如果您正苦于以下问题:Java NumberFormat.setCurrency方法的具体用法?Java NumberFormat.setCurrency怎么用?Java NumberFormat.setCurrency使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.text.NumberFormat的用法示例。


在下文中一共展示了NumberFormat.setCurrency方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: format

import java.text.NumberFormat; //导入方法依赖的package包/类
/**
 * Formats Money into a human readable currency string.
 *
 * @param moneyOrNull the money to format, or null
 * @param locale the {@link Locale} to format for
 * @return a formatted money string, or null if moneyOrNull is null
 */
public static String format(Money moneyOrNull, Locale locale) {
  if (moneyOrNull == null) {
    return null;
  }

  // Convert the currency specified in the proto to a Currency object.
  Currency currency = Currency.getInstance(moneyOrNull.getCurrency().toString());

  // Create a formatter that uses the currency.
  NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
  formatter.setCurrency(currency);
  formatter.setMaximumFractionDigits(currency.getDefaultFractionDigits());

  if (currency.getDefaultFractionDigits() == 0) {
    // For locales that do not support fractional amounts, use the amount as is. For example,
    // Japan has no concept of "cents". The base unit of currency is 1 Yen, and currency is always
    // measured in Yen.
    return formatter.format(moneyOrNull.getAmount());
  } else {
    // For locales that support fractional amounts, divide by 100 to get the local equivalent of
    // "dollars". For example, the amount in USD is represented as cents. We devide by 100 to get
    // US dollars.
    return formatter.format(moneyOrNull.getAmount() / 100.0);
  }
}
 
开发者ID:square,项目名称:catalog-api-demo,代码行数:33,代码来源:Moneys.java

示例2: convertOutbound

import java.text.NumberFormat; //导入方法依赖的package包/类
public OutboundVariable convertOutbound(Object data, OutboundContext outctx)
		throws MarshallException {
	Double out = 0.0;
	// Error out if an unsupported class is passed
	if (!(data instanceof Currency)) {
		log.warn("Unsupported input. Class=" + data.getClass() + ", data="
				+ data);
		throw new MarshallException(data.getClass());
	}
	String currency = data.toString();
	if (currency != null && currency != "") {
		String layout = CurrencyFieldMetaData.getCurrencyFormat();
		NumberFormat fmt;
		if (LocaleContext.getLocale() != null) {
			fmt = NumberFormat.getCurrencyInstance(LocaleContext
					.getLocale());
		} else {
			fmt = NumberFormat.getCurrencyInstance();
		}
		fmt.setCurrency(java.util.Currency.getInstance(Currency.USD));
		if (layout != null && layout.length() > 0
				&& fmt instanceof DecimalFormat) {
			((DecimalFormat) fmt).applyPattern(layout);
		}
		try {
			out = Double.valueOf((fmt.parse(currency)).doubleValue());
			currency = out.toString();
		} catch (ParseException e) {
		}
	}
	return new SimpleOutboundVariable(currency, outctx, true);
}
 
开发者ID:jaffa-projects,项目名称:jaffa-framework,代码行数:33,代码来源:CurrencyConverter.java

示例3: testFormatting

import java.text.NumberFormat; //导入方法依赖的package包/类
static void testFormatting() {
    boolean failed = false;
    Locale[] locales = {
        Locale.US,
        Locale.JAPAN,
        Locale.GERMANY,
        Locale.ITALY,
        new Locale("it", "IT", "EURO") };
    Currency[] currencies = {
        null,
        Currency.getInstance("USD"),
        Currency.getInstance("JPY"),
        Currency.getInstance("DEM"),
        Currency.getInstance("EUR"),
    };
    String[][] expecteds = {
        {"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
        {"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
        {"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
    };

    for (int i = 0; i < locales.length; i++) {
        Locale locale = locales[i];
        NumberFormat format = NumberFormat.getCurrencyInstance(locale);
        for (int j = 0; j < currencies.length; j++) {
            Currency currency = currencies[j];
            String expected = expecteds[i][j];
            if (currency != null) {
                format.setCurrency(currency);
                int digits = currency.getDefaultFractionDigits();
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            }
            String result = format.format(1234.56);
            if (!result.equals(expected)) {
                failed = true;
                System.out.println("FAIL: Locale " + locale
                    + (currency == null ? ", default currency" : (", currency: " + currency))
                    + ", expected: " + expected
                    + ", actual: " + result);
            }
        }
    }

    if (failed) {
        throw new RuntimeException();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:51,代码来源:CurrencyFormat.java


注:本文中的java.text.NumberFormat.setCurrency方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。