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

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

本文整理了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

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

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

  1. private String getCurrencyCode() {
  2. return Currency.getInstance(Locale.getDefault()).getCurrencyCode();
  3. }
  4. }

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

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

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

  1. System.out.println(
  2. Currency.getInstance("USD").getSymbol(Locale.US)
  3. );
  4. // prints $
  5. System.out.println(
  6. Currency.getInstance("USD").getSymbol(Locale.FRANCE)
  7. );
  8. // prints USD
  9. System.out.println(
  10. Currency.getInstance("EUR").getSymbol(Locale.US)
  11. );
  12. // prints EUR
  13. System.out.println(
  14. Currency.getInstance("EUR").getSymbol(Locale.FRANCE)
  15. );
  16. // prints €

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

  1. /**
  2. * Sets the currency.
  3. * <p>
  4. * The international currency symbol and the currency symbol are updated,
  5. * but the min and max number of fraction digits stays the same.
  6. * <p>
  7. *
  8. * @param currency
  9. * the new currency.
  10. * @throws NullPointerException
  11. * if {@code currency} is {@code null}.
  12. */
  13. public void setCurrency(Currency currency) {
  14. if (currency == null) {
  15. throw new NullPointerException("currency == null");
  16. }
  17. if (currency == this.currency) {
  18. return;
  19. }
  20. this.currency = currency;
  21. intlCurrencySymbol = currency.getCurrencyCode();
  22. currencySymbol = currency.getSymbol(locale);
  23. }

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

  1. String s = number.toPlainString();
  2. return s.startsWith("0.") ? s.substring(1) : s;
  3. } else if (formatUp.equals("TME")) {
  4. int pow = number.precision() - number.scale() - 1;
  5. number = number.movePointLeft(pow);
  6. return number.toPlainString() + "E" +
  7. DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
  8. char localGrouping = symbols.getGroupingSeparator();
  9. char localDecimal = symbols.getDecimalSeparator();
  10. Currency currency = Currency.getInstance(Locale.getDefault());
  11. output.insert(0, currency.getCurrencyCode());
  12. maxLength += 6;
  13. } else if (c == 'L' || c == 'l' || c == 'U' || c == 'u') {
  14. Currency currency = Currency.getInstance(Locale.getDefault());
  15. output.insert(0, currency.getSymbol());
  16. maxLength += 9;
  17. } else if (c == '$') {
  18. Currency currency = Currency.getInstance(Locale.getDefault());
  19. String cs = currency.getSymbol();
  20. output.insert(0, cs);
  21. } else {

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

  1. /**
  2. * Returns the unit amount (e.g. .01 for US)
  3. * @param currency
  4. * @return
  5. */
  6. public Money getUnitAmount(Money difference) {
  7. Currency currency = difference.getCurrency();
  8. BigDecimal divisor = new BigDecimal(Math.pow(10, currency.getDefaultFractionDigits()));
  9. BigDecimal unitAmount = new BigDecimal("1").divide(divisor);
  10. if (difference.lessThan(BigDecimal.ZERO)) {
  11. unitAmount = unitAmount.negate();
  12. }
  13. return new Money(unitAmount, currency);
  14. }

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

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

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

  1. public class CurrencyTest {
  2. public static void main(String[] args) throws Exception {
  3. Locale defaultLocale = Locale.getDefault();
  4. displayCurrencyInfoForLocale(defaultLocale);
  5. Locale swedishLocale = new Locale("sv", "SE");
  6. displayCurrencyInfoForLocale(swedishLocale);
  7. }
  8. public static void displayCurrencyInfoForLocale(Locale locale) {
  9. System.out.println("Locale: " + locale.getDisplayName());
  10. Currency currency = Currency.getInstance(locale);
  11. System.out.println("Currency Code: " + currency.getCurrencyCode());
  12. System.out.println("Symbol: " + currency.getSymbol());
  13. System.out.println("Default Fraction Digits: " + currency.getDefaultFractionDigits());
  14. System.out.println();
  15. }
  16. }

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

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

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

  1. assertTrue(immutability.test(Character.valueOf('a')));
  2. assertTrue(immutability.test(this.getClass()));
  3. assertTrue(immutability.test(Currency.getInstance(Locale.US)));
  4. assertTrue(immutability.test(Locale.getDefault()));
  5. assertTrue(immutability.test(Byte.valueOf(Integer.valueOf(1).byteValue())));
  6. assertTrue(immutability.test(Short.valueOf(Integer.valueOf(1).shortValue())));
  7. assertTrue(immutability.test(Double.valueOf(1)));
  8. assertTrue(immutability.test(BigInteger.valueOf(1)));
  9. assertTrue(immutability.test(BigDecimal.valueOf(1)));
  10. assertTrue(immutability.test(InetAddress.getLocalHost()));
  11. assertTrue(immutability.test(new InetSocketAddress(InetAddress.getLocalHost(), 80)));

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

  1. /**
  2. * 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
  3. * system property.
  4. * @return The default currency to use when none is specified
  5. */
  6. public static Currency defaultCurrency() {
  7. if (CurrencyConsiderationContext.getCurrencyConsiderationContext() != null &&
  8. CurrencyConsiderationContext.getCurrencyConsiderationContext().size() > 0 &&
  9. CurrencyConsiderationContext.getCurrencyDeterminationService() != null) {
  10. return Currency.getInstance(CurrencyConsiderationContext.getCurrencyDeterminationService().getCurrencyCode(CurrencyConsiderationContext.getCurrencyConsiderationContext()));
  11. }
  12. // Check the BLC Thread
  13. BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
  14. if (brc != null && brc.getBroadleafCurrency() != null) {
  15. assert brc.getBroadleafCurrency().getCurrencyCode() != null;
  16. return Currency.getInstance(brc.getBroadleafCurrency().getCurrencyCode());
  17. }
  18. if (System.getProperty("currency.default") != null) {
  19. return Currency.getInstance(System.getProperty("currency.default"));
  20. }
  21. Locale locale = Locale.getDefault();
  22. if (locale.getCountry() != null && locale.getCountry().length() == 2) {
  23. return Currency.getInstance(locale);
  24. }
  25. return Currency.getInstance("USD");
  26. }

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

  1. BigDecimal converted = new BigDecimal(object.toString());
  2. return Optional.of(converted);
  3. } catch (NumberFormatException e) {
  4. return Optional.of(Locale.forLanguageTag(object.toString().replace('_', '-')));
  5. } catch (IllegalArgumentException e) {
  6. context.reject(object, e);
  7. return Optional.of(Currency.getInstance(object.toString()));
  8. } catch (IllegalArgumentException e) {
  9. context.reject(object, e);
  10. return Optional.of(((BigDecimal) object).toBigInteger());
  11. return Optional.of(new BigDecimal(object.toString()));

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

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

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

  1. public static String formatCurrency(Number number, Locale locale) {
  2. Currency currency = Currency.getInstance(locale);
  3. NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
  4. numberFormat.setCurrency(currency);
  5. numberFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
  6. String s = numberFormat.format(number);
  7. s = s.replace(currency.getCurrencyCode(), currency.getSymbol(locale));
  8. return s;
  9. }

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

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

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

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

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

  1. public class CurrencyTest
  2. {
  3. @Test
  4. public void testGetNumberFormatForCurrencyCode()
  5. {
  6. NumberFormat format = NumberFormat.getInstance();
  7. format.setMaximumFractionDigits(2);
  8. Currency currency = Currency.getInstance("USD");
  9. format.setCurrency(currency);
  10. System.out.println(format.format(1234.23434));
  11. }
  12. }

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

  1. protected java.text.DecimalFormatSymbols initialValue() {
  2. java.text.DecimalFormatSymbols dfSymbols = new java.text.DecimalFormatSymbols(
  3. new Locale("pt", "BR"));
  4. dfSymbols.setZeroDigit('0');
  5. dfSymbols.setDecimalSeparator(',');
  6. dfSymbols.setMonetaryDecimalSeparator(',');
  7. dfSymbols.setDigit('#');
  8. dfSymbols.setGroupingSeparator('.');
  9. dfSymbols.setCurrency(Currency.getInstance(new Locale("pt", "BR")));
  10. return dfSymbols;
  11. }

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

  1. java.util.Currency usd = java.util.Currency.getInstance("USD");
  2. java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(
  3. java.util.Locale.JAPAN);
  4. format.setCurrency(usd);
  5. System.out.println(format.format(23.23));
  6. format.setMaximumFractionDigits(usd.getDefaultFractionDigits());
  7. System.out.println(format.format(23.23));

相关文章