Java Code Examples for java.util.Currency#getDefaultFractionDigits()

The following examples show how to use java.util.Currency#getDefaultFractionDigits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CurrencyValue.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
   * Returns a string representing the currency value such as "3.14,USD" for
   * a CurrencyValue of $3.14 USD.
   */
  public String strValue() {
    int digits = 0;
    try {
      Currency currency =
        Currency.getInstance(this.getCurrencyCode());
      if (currency == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            "Invalid currency code " + this.getCurrencyCode());
  }
      digits = currency.getDefaultFractionDigits();
}
    catch(IllegalArgumentException exception) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Invalid currency code " + this.getCurrencyCode());
    }

    String amount = Long.toString(this.getAmount());
    if (this.getAmount() == 0) {
      amount += "000000".substring(0,digits);
    }
    return
      amount.substring(0, amount.length() - digits)
      + "." + amount.substring(amount.length() - digits)
      + "," +  this.getCurrencyCode();
  }
 
Example 2
Source File: NumberFormatProviderImpl.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 3
Source File: NumberFormatProviderImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 4
Source File: NumberFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 5
Source File: NumberFormatProviderImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 6
Source File: DecimalFormat.java    From jdk-1.7-annotated with Apache License 2.0 6 votes vote down vote up
/**
 * Adjusts the minimum and maximum fraction digits to values that
 * are reasonable for the currency's default fraction digits.
 */
void adjustForCurrencyDefaultFractionDigits() {
    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 = getMinimumFractionDigits();
            // Common patterns are "#.##", "#.00", "#".
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == getMaximumFractionDigits()) {
                setMinimumFractionDigits(digits);
                setMaximumFractionDigits(digits);
            } else {
                setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                setMaximumFractionDigits(digits);
            }
        }
    }
}
 
Example 7
Source File: NumberFormatProviderImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 8
Source File: Bug4512215.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 9
Source File: Bug4512215.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 10
Source File: Bug4512215.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 11
Source File: Bug4512215.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 12
Source File: Bug4512215.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 13
Source File: CurrencyEditText.java    From CurrencyEditText with Apache License 2.0 5 votes vote down vote up
private void init(){
    this.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

    currentLocale = retrieveLocale();
    Currency currentCurrency = getCurrencyForLocale(currentLocale);
    decimalDigits = currentCurrency.getDefaultFractionDigits();
    initCurrencyTextWatcher();
}
 
Example 14
Source File: Bug4512215.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
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());
    }
}
 
Example 15
Source File: Bug4512215.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 16
Source File: Bug4512215.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 17
Source File: Bug4512215.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 18
Source File: CurrencyFormat.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 19
Source File: CurrencyFormat.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}
 
Example 20
Source File: CurrencyFormat.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
    }
}