当前位置: 首页>>代码示例>>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;未经允许,请勿转载。