java.util.Currency类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(189)

本文整理了Java中java.util.Currency类的一些代码示例,展示了Currency类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Currency类的具体详情如下:
包路径:java.util.Currency
类名称:Currency

Currency介绍

[英]A currency corresponding to an ISO 4217 currency code such as "EUR" or "USD".
[中]与ISO 4217货币代码(如“EUR”或“USD”)相对应的一种货币。

代码示例

代码示例来源:origin: stackoverflow.com

java.util.Currency usd = java.util.Currency.getInstance("USD");
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
format.setCurrency(usd);
System.out.println(format.format(23));

代码示例来源:origin: spotbugs/spotbugs

private String getCurrencyCode() {
    return Currency.getInstance(Locale.getDefault()).getCurrencyCode();
  }
}

代码示例来源:origin: robovm/robovm

/**
 * Equivalent to {@code getSymbol(Locale.getDefault())}.
 * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
 */
public String getSymbol() {
  return getSymbol(Locale.getDefault());
}

代码示例来源:origin: stackoverflow.com

System.out.println(
  Currency.getInstance("USD").getSymbol(Locale.US)
);
// prints $

System.out.println(
  Currency.getInstance("USD").getSymbol(Locale.FRANCE)
);
// prints USD

System.out.println(
  Currency.getInstance("EUR").getSymbol(Locale.US)
);
// prints EUR

System.out.println(
  Currency.getInstance("EUR").getSymbol(Locale.FRANCE)
);
// prints €

代码示例来源:origin: robovm/robovm

/**
 * Sets the currency.
 * <p>
 * The international currency symbol and the currency symbol are updated,
 * but the min and max number of fraction digits stays the same.
 * <p>
 *
 * @param currency
 *            the new currency.
 * @throws NullPointerException
 *             if {@code currency} is {@code null}.
 */
public void setCurrency(Currency currency) {
  if (currency == null) {
    throw new NullPointerException("currency == null");
  }
  if (currency == this.currency) {
    return;
  }
  this.currency = currency;
  intlCurrencySymbol = currency.getCurrencyCode();
  currencySymbol = currency.getSymbol(locale);
}

代码示例来源:origin: com.h2database/h2

String s = number.toPlainString();
  return s.startsWith("0.") ? s.substring(1) : s;
} else if (formatUp.equals("TME")) {
  int pow = number.precision() - number.scale() - 1;
  number = number.movePointLeft(pow);
  return number.toPlainString() + "E" +
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
char localGrouping = symbols.getGroupingSeparator();
char localDecimal = symbols.getDecimalSeparator();
    Currency currency = Currency.getInstance(Locale.getDefault());
    output.insert(0, currency.getCurrencyCode());
    maxLength += 6;
  } else if (c == 'L' || c == 'l' || c == 'U' || c == 'u') {
    Currency currency = Currency.getInstance(Locale.getDefault());
    output.insert(0, currency.getSymbol());
    maxLength += 9;
  } else if (c == '$') {
    Currency currency = Currency.getInstance(Locale.getDefault());
    String cs = currency.getSymbol();
    output.insert(0, cs);
  } else {

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

/**
 * Returns the unit amount (e.g. .01 for US)
 * @param currency
 * @return
 */
public Money getUnitAmount(Money difference) {
  Currency currency = difference.getCurrency();
  BigDecimal divisor = new BigDecimal(Math.pow(10, currency.getDefaultFractionDigits()));
  BigDecimal unitAmount = new BigDecimal("1").divide(divisor);
  if (difference.lessThan(BigDecimal.ZERO)) {
    unitAmount = unitAmount.negate();
  }
  return new Money(unitAmount, currency);
}

代码示例来源:origin: hibernate/hibernate-orm

public static MonetaryAmount fromString(String amount, String currencyCode) {
  return new MonetaryAmount(new BigDecimal(amount),
               Currency.getInstance(currencyCode));
}

代码示例来源:origin: stackoverflow.com

public class CurrencyTest {
  public static void main(String[] args) throws Exception {
    Locale defaultLocale = Locale.getDefault();
    displayCurrencyInfoForLocale(defaultLocale);

    Locale swedishLocale = new Locale("sv", "SE");
    displayCurrencyInfoForLocale(swedishLocale);
  }

  public static void displayCurrencyInfoForLocale(Locale locale) {
    System.out.println("Locale: " + locale.getDisplayName());
    Currency currency = Currency.getInstance(locale);
    System.out.println("Currency Code: " + currency.getCurrencyCode());
    System.out.println("Symbol: " + currency.getSymbol());
    System.out.println("Default Fraction Digits: " + currency.getDefaultFractionDigits());
    System.out.println();
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

/**
 * Returns the unit amount (e.g. .01 for US and all other 2 decimal currencies)
 *
 * @param blCurrency
 * @return
 */
public static Money getUnitAmount(BroadleafCurrency blCurrency) {
  Currency currency = getCurrency(blCurrency);
  BigDecimal divisor = new BigDecimal(Math.pow(10, currency.getDefaultFractionDigits()));
  BigDecimal unitAmount = new BigDecimal("1").divide(divisor);
  return new Money(unitAmount, currency);
}

代码示例来源:origin: wildfly/wildfly

assertTrue(immutability.test(Character.valueOf('a')));
assertTrue(immutability.test(this.getClass()));
assertTrue(immutability.test(Currency.getInstance(Locale.US)));
assertTrue(immutability.test(Locale.getDefault()));
assertTrue(immutability.test(Byte.valueOf(Integer.valueOf(1).byteValue())));
assertTrue(immutability.test(Short.valueOf(Integer.valueOf(1).shortValue())));
assertTrue(immutability.test(Double.valueOf(1)));
assertTrue(immutability.test(BigInteger.valueOf(1)));
assertTrue(immutability.test(BigDecimal.valueOf(1)));
assertTrue(immutability.test(InetAddress.getLocalHost()));
assertTrue(immutability.test(new InetSocketAddress(InetAddress.getLocalHost(), 80)));

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

/**
 * Attempts to load a default currency by using the default locale. {@link Currency#getInstance(Locale)} uses the country component of the locale to resolve the currency. In some instances, the locale may not have a country component, in which case the default currency can be controlled with a
 * system property.
 * @return The default currency to use when none is specified
 */
public static Currency defaultCurrency() {
  if (CurrencyConsiderationContext.getCurrencyConsiderationContext() != null &&
      CurrencyConsiderationContext.getCurrencyConsiderationContext().size() > 0 &&
      CurrencyConsiderationContext.getCurrencyDeterminationService() != null) {
    return Currency.getInstance(CurrencyConsiderationContext.getCurrencyDeterminationService().getCurrencyCode(CurrencyConsiderationContext.getCurrencyConsiderationContext()));
  }
  // Check the BLC Thread
  BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
  if (brc != null && brc.getBroadleafCurrency() != null) {
    assert brc.getBroadleafCurrency().getCurrencyCode() != null;
    return Currency.getInstance(brc.getBroadleafCurrency().getCurrencyCode());
  }
  if (System.getProperty("currency.default") != null) {
    return Currency.getInstance(System.getProperty("currency.default"));
  }
  Locale locale = Locale.getDefault();
  if (locale.getCountry() != null && locale.getCountry().length() == 2) {
    return Currency.getInstance(locale);
  }
  return Currency.getInstance("USD");
}

代码示例来源:origin: micronaut-projects/micronaut-core

BigDecimal converted = new BigDecimal(object.toString());
  return Optional.of(converted);
} catch (NumberFormatException e) {
  return Optional.of(Locale.forLanguageTag(object.toString().replace('_', '-')));
} catch (IllegalArgumentException e) {
  context.reject(object, e);
  return Optional.of(Currency.getInstance(object.toString()));
} catch (IllegalArgumentException e) {
  context.reject(object, e);
    return Optional.of(((BigDecimal) object).toBigInteger());
  return Optional.of(new BigDecimal(object.toString()));

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

/**
 * Returns the java.util.Currency constructed from the org.broadleafcommerce.common.currency.domain.BroadleafCurrency.
 * If there is no BroadleafCurrency specified this will return the currency based on the JVM locale
 * 
 * @return
 */
public Currency getJavaCurrency() {
  if (javaCurrency == null) {
    try {
      if (getBroadleafCurrency() != null && getBroadleafCurrency().getCurrencyCode() != null) {
        javaCurrency = getBroadleafCurrency().getJavaCurrency();
      } else {
        javaCurrency = Currency.getInstance(getJavaLocale());
      }
    } catch (IllegalArgumentException e) {
      LOG.warn("There was an error processing the configured locale into the java currency. This is likely because the default" +
          " locale is set to something like 'en' (which is NOT apart of ISO 3166 and does not have a currency" +
          " associated with it) instead of 'en_US' (which IS apart of ISO 3166 and has a currency associated" +
          " with it). Because of this, the currency is now set to the default locale of the JVM");
      LOG.warn("To fully resolve this, update the default entry in the BLC_LOCALE table to take into account the" +
          " country code as well as the language. Alternatively, you could also update the BLC_CURRENCY table" +
          " to contain a default currency.");
      javaCurrency = Currency.getInstance(java.util.Locale.getDefault());
    }
  }
  return javaCurrency;
}

代码示例来源:origin: com.google.code.maven-play-plugin.org.playframework/play

public static String formatCurrency(Number number, Locale locale) {
  Currency currency = Currency.getInstance(locale);
  NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
  numberFormat.setCurrency(currency);
  numberFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
  String s = numberFormat.format(number);
  s = s.replace(currency.getCurrencyCode(), currency.getSymbol(locale));
  return s;
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

public Money(int amount, String currencyCode) {
  this(BigDecimal.valueOf(amount).setScale(BankersRounding.getScaleForCurrency(Currency.getInstance(currencyCode)), RoundingMode.HALF_EVEN), Currency.getInstance(currencyCode));
}

代码示例来源:origin: prestodb/presto

case STD_CURRENCY:
  return Currency.getInstance(value);
case STD_PATTERN:
      return new Locale(value);
    ix = _firstHyphenOrUnderscore(value);
    if (ix < 0) { // two pieces
      return new Locale(first, value);
    return new Locale(first, second, value.substring(ix+1));

代码示例来源:origin: stackoverflow.com

public class CurrencyTest
{
  @Test
  public void testGetNumberFormatForCurrencyCode()
  {
    NumberFormat format = NumberFormat.getInstance();
    format.setMaximumFractionDigits(2);
    Currency currency = Currency.getInstance("USD");
    format.setCurrency(currency);

    System.out.println(format.format(1234.23434));
  }   
}

代码示例来源:origin: br.com.anteros/Anteros-Core

protected java.text.DecimalFormatSymbols initialValue() {
  java.text.DecimalFormatSymbols dfSymbols = new java.text.DecimalFormatSymbols(
      new Locale("pt", "BR"));
  dfSymbols.setZeroDigit('0');
  dfSymbols.setDecimalSeparator(',');
  dfSymbols.setMonetaryDecimalSeparator(',');
  dfSymbols.setDigit('#');
  dfSymbols.setGroupingSeparator('.');
  dfSymbols.setCurrency(Currency.getInstance(new Locale("pt", "BR")));
  return dfSymbols;
}

代码示例来源:origin: stackoverflow.com

java.util.Currency usd = java.util.Currency.getInstance("USD");
 java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(
    java.util.Locale.JAPAN);
 format.setCurrency(usd);
 System.out.println(format.format(23.23));
 format.setMaximumFractionDigits(usd.getDefaultFractionDigits());
 System.out.println(format.format(23.23));

相关文章