當前位置: 首頁>>代碼示例>>Java>>正文


Java Currency.getDefaultFractionDigits方法代碼示例

本文整理匯總了Java中java.util.Currency.getDefaultFractionDigits方法的典型用法代碼示例。如果您正苦於以下問題:Java Currency.getDefaultFractionDigits方法的具體用法?Java Currency.getDefaultFractionDigits怎麽用?Java Currency.getDefaultFractionDigits使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.Currency的用法示例。


在下文中一共展示了Currency.getDefaultFractionDigits方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: adjustForCurrencyDefaultFractionDigits

import java.util.Currency; //導入方法依賴的package包/類
/**
 * Adjusts the minimum and maximum fraction digits to values that
 * are reasonable for the currency's default fraction digits.
 */
private static void adjustForCurrencyDefaultFractionDigits(
        DecimalFormat format, DecimalFormatSymbols symbols) {
    Currency currency = symbols.getCurrency();
    if (currency == null) {
        try {
            currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
        } catch (IllegalArgumentException e) {
        }
    }
    if (currency != null) {
        int digits = currency.getDefaultFractionDigits();
        if (digits != -1) {
            int oldMinDigits = format.getMinimumFractionDigits();
            // Common patterns are "#.##", "#.00", "#".
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == format.getMaximumFractionDigits()) {
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            } else {
                format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                format.setMaximumFractionDigits(digits);
            }
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:30,代碼來源:NumberFormatProviderImpl.java

示例2: format

import java.util.Currency; //導入方法依賴的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

示例3: MoneyLabel

import java.util.Currency; //導入方法依賴的package包/類
public MoneyLabel(BigDecimal value, Currency currency, boolean showSymbol, boolean showCode, boolean html)
{
	super(value);
	final int frac = currency.getDefaultFractionDigits();
	if( frac != -1 )
	{
		setMinDecimals(frac);
		setMaxDecimals(frac);
	}
	this.currency = currency;
	this.showSymbol = showSymbol;
	this.showCode = showCode;
	this.html = html;
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:15,代碼來源:MoneyLabel.java

示例4: testCurrencyDefined

import java.util.Currency; //導入方法依賴的package包/類
private static void testCurrencyDefined(String currencyCode, int digits) {
    Currency currency = Currency.getInstance(currencyCode);
    if (currency.getDefaultFractionDigits() != digits) {
        throw new RuntimeException("[" + currencyCode
                + "] expected: " + digits
                + "; got: " + currency.getDefaultFractionDigits());
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:9,代碼來源:Bug4512215.java

示例5: next2

import java.util.Currency; //導入方法依賴的package包/類
public float next2(DataContext context) {
    Currency currency = Currency.getInstance(Locale.getDefault());
    float cents = 0;
    if (currency.getDefaultFractionDigits() == 2) {
        if (useCommon) {
            cents = common[random.nextInt(common.length)];
        } else {
            cents = (float) random.nextInt(100) / 100;
        }
    }
    return Math.min(max, random.nextInt((int) Math.floor(max + 1)) + cents);
}
 
開發者ID:ZieIony,項目名稱:RandomData,代碼行數:13,代碼來源:AmountGenerator.java

示例6: getExponent

import java.util.Currency; //導入方法依賴的package包/類
private static int getExponent(@NonNull String currencyCode) {
    // first some overrides:
    if ("ISK".equals(currencyCode)) {
        return 2;
    }
    if ("CLP".equals(currencyCode)) {
        return 2;
    }
    if ("MXP".equals(currencyCode)) {
        return 2;
    }
    if ("MRO".equals(currencyCode)) {
        return 1;
    }
    if ("IDR".equals(currencyCode)) {
        return 0;
    }
    if ("VND".equals(currencyCode)) {
        return 0;
    }
    if ("UGX".equals(currencyCode)) {
        return 0;
    }
    if ("CVE".equals(currencyCode)) {
        return 0;
    }
    if ("ZMW".equals(currencyCode)) {
        return 2;
    }
    if ("GHC".equals(currencyCode)) {
        return 0;
    }
    if ("BYR".equals(currencyCode)) {
        return 0;
    }
    if ("BYN".equals(currencyCode)) {
        return 2;
    }
    if ("RSD".equals(currencyCode)) {
        return 2;
    }

    // now the default behavior
    Currency curr = null;
    try {
        curr = Currency.getInstance(currencyCode);
    } catch (final IllegalArgumentException exception) {
        Log.d(TAG, "Currency is incorrect: ", exception);
    }
    int exponent = 0;
    if (curr != null) {
        exponent = curr.getDefaultFractionDigits();
        if (exponent == -1) {
            exponent = 0;
        }
    }
    return exponent;
}
 
開發者ID:Adyen,項目名稱:adyen-android,代碼行數:59,代碼來源:AmountUtil.java

示例7: getPerLitreFractionDigits

import java.util.Currency; //導入方法依賴的package包/類
public static int getPerLitreFractionDigits(Currency currency) {
    checkPropertiesAreLoaded();
    if (properties.containsKey(currency.getCurrencyCode()))
        return Integer.valueOf(properties.getProperty(currency.getCurrencyCode()).split(DELIMETER)[FRACTIONS_PER_LITRE]);
    return currency.getDefaultFractionDigits() + 1;
}
 
開發者ID:piskula,項目名稱:FuelUp,代碼行數:7,代碼來源:CurrencyUtil.java

示例8: Money

import java.util.Currency; //導入方法依賴的package包/類
/**
    * The constructor does not complex computations and requires simple, inputs
    * consistent with the class invariant. Other creation methods are available
    * for convenience.
    */
public Money(BigDecimal amount, Currency currency) {
	if (amount.scale() != currency.getDefaultFractionDigits()) throw new IllegalArgumentException("Scale of amount does not match currency");
	this.currency = currency;
	this.amount = amount;
}
 
開發者ID:neuhalje,項目名稱:TimeAndMoney,代碼行數:11,代碼來源:Money.java

示例9: testFormatting

import java.util.Currency; //導入方法依賴的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.util.Currency.getDefaultFractionDigits方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。