📄 numberformat.java
字号:
* The default format is one of the styles provided by the other * factory methods: getNumberInstance, getIntegerInstance, * getCurrencyInstance or getPercentInstance. * Exactly which one is locale dependant. */ public final static NumberFormat getInstance() { return getInstance(Locale.getDefault(), NUMBERSTYLE); } /** * Returns the default number format for the specified locale. * The default format is one of the styles provided by the other * factory methods: getNumberInstance, getIntegerInstance, * getCurrencyInstance or getPercentInstance. * Exactly which one is locale dependant. */ public static NumberFormat getInstance(Locale inLocale) { return getInstance(inLocale, NUMBERSTYLE); } /** * Returns a general-purpose number format for the current default locale. */ public final static NumberFormat getNumberInstance() { return getInstance(Locale.getDefault(), NUMBERSTYLE); } /** * Returns a general-purpose number format for the specified locale. */ public static NumberFormat getNumberInstance(Locale inLocale) { return getInstance(inLocale, NUMBERSTYLE); } /** * Returns an integer number format for the current default locale. The * returned number format is configured to round floating point numbers * to the nearest integer using IEEE half-even rounding (see {@link * java.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting, * and to parse only the integer part of an input string (see {@link * #isParseIntegerOnly isParseIntegerOnly}). * * @return a number format for integer values * @since 1.4 */ public final static NumberFormat getIntegerInstance() { return getInstance(Locale.getDefault(), INTEGERSTYLE); } /** * Returns an integer number format for the specified locale. The * returned number format is configured to round floating point numbers * to the nearest integer using IEEE half-even rounding (see {@link * java.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting, * and to parse only the integer part of an input string (see {@link * #isParseIntegerOnly isParseIntegerOnly}). * * @param inLocale the locale for which a number format is needed * @return a number format for integer values * @since 1.4 */ public static NumberFormat getIntegerInstance(Locale inLocale) { return getInstance(inLocale, INTEGERSTYLE); } /** * Returns a currency format for the current default locale. */ public final static NumberFormat getCurrencyInstance() { return getInstance(Locale.getDefault(), CURRENCYSTYLE); } /** * Returns a currency format for the specified locale. */ public static NumberFormat getCurrencyInstance(Locale inLocale) { return getInstance(inLocale, CURRENCYSTYLE); } /** * Returns a percentage format for the current default locale. */ public final static NumberFormat getPercentInstance() { return getInstance(Locale.getDefault(), PERCENTSTYLE); } /** * Returns a percentage format for the specified locale. */ public static NumberFormat getPercentInstance(Locale inLocale) { return getInstance(inLocale, PERCENTSTYLE); } /** * Returns a scientific format for the current default locale. */ /*public*/ final static NumberFormat getScientificInstance() { return getInstance(Locale.getDefault(), SCIENTIFICSTYLE); } /** * Returns a scientific format for the specified locale. */ /*public*/ static NumberFormat getScientificInstance(Locale inLocale) { return getInstance(inLocale, SCIENTIFICSTYLE); } /** * Get the set of Locales for which NumberFormats are installed * @return available locales */ public static Locale[] getAvailableLocales() { return LocaleData.getAvailableLocales("NumberPatterns"); } /** * Overrides hashCode */ public int hashCode() { return maximumIntegerDigits * 37 + maxFractionDigits; // just enough fields for a reasonable distribution } /** * Overrides equals */ public boolean equals(Object obj) { if (obj == null) return false; if (this == obj) return true; if (getClass() != obj.getClass()) return false; NumberFormat other = (NumberFormat) obj; return (maximumIntegerDigits == other.maximumIntegerDigits && minimumIntegerDigits == other.minimumIntegerDigits && maximumFractionDigits == other.maximumFractionDigits && minimumFractionDigits == other.minimumFractionDigits && groupingUsed == other.groupingUsed && parseIntegerOnly == other.parseIntegerOnly); } /** * Overrides Cloneable */ public Object clone() { NumberFormat other = (NumberFormat) super.clone(); return other; } /** * Returns true if grouping is used in this format. For example, in the * English locale, with grouping on, the number 1234567 might be formatted * as "1,234,567". The grouping separator as well as the size of each group * is locale dependant and is determined by sub-classes of NumberFormat. * @see #setGroupingUsed */ public boolean isGroupingUsed() { return groupingUsed; } /** * Set whether or not grouping will be used in this format. * @see #isGroupingUsed */ public void setGroupingUsed(boolean newValue) { groupingUsed = newValue; } /** * Returns the maximum number of digits allowed in the integer portion of a * number. * @see #setMaximumIntegerDigits */ public int getMaximumIntegerDigits() { return maximumIntegerDigits; } /** * Sets the maximum number of digits allowed in the integer portion of a * number. maximumIntegerDigits must be >= minimumIntegerDigits. If the * new value for maximumIntegerDigits is less than the current value * of minimumIntegerDigits, then minimumIntegerDigits will also be set to * the new value. * @param newValue the maximum number of integer digits to be shown; if * less than zero, then zero is used. The concrete subclass may enforce an * upper limit to this value appropriate to the numeric type being formatted. * @see #getMaximumIntegerDigits */ public void setMaximumIntegerDigits(int newValue) { maximumIntegerDigits = Math.max(0,newValue); if (minimumIntegerDigits > maximumIntegerDigits) minimumIntegerDigits = maximumIntegerDigits; } /** * Returns the minimum number of digits allowed in the integer portion of a * number. * @see #setMinimumIntegerDigits */ public int getMinimumIntegerDigits() { return minimumIntegerDigits; } /** * Sets the minimum number of digits allowed in the integer portion of a * number. minimumIntegerDigits must be <= maximumIntegerDigits. If the * new value for minimumIntegerDigits exceeds the current value * of maximumIntegerDigits, then maximumIntegerDigits will also be set to * the new value * @param newValue the minimum number of integer digits to be shown; if * less than zero, then zero is used. The concrete subclass may enforce an * upper limit to this value appropriate to the numeric type being formatted. * @see #getMinimumIntegerDigits */ public void setMinimumIntegerDigits(int newValue) { minimumIntegerDigits = Math.max(0,newValue); if (minimumIntegerDigits > maximumIntegerDigits) maximumIntegerDigits = minimumIntegerDigits; } /** * Returns the maximum number of digits allowed in the fraction portion of a * number. * @see #setMaximumFractionDigits */ public int getMaximumFractionDigits() { return maximumFractionDigits; } /** * Sets the maximum number of digits allowed in the fraction portion of a * number. maximumFractionDigits must be >= minimumFractionDigits. If the * new value for maximumFractionDigits is less than the current value * of minimumFractionDigits, then minimumFractionDigits will also be set to * the new value. * @param newValue the maximum number of fraction digits to be shown; if * less than zero, then zero is used. The concrete subclass may enforce an * upper limit to this value appropriate to the numeric type being formatted. * @see #getMaximumFractionDigits */ public void setMaximumFractionDigits(int newValue) { maximumFractionDigits = Math.max(0,newValue); if (maximumFractionDigits < minimumFractionDigits) minimumFractionDigits = maximumFractionDigits; } /** * Returns the minimum number of digits allowed in the fraction portion of a * number. * @see #setMinimumFractionDigits */ public int getMinimumFractionDigits() { return minimumFractionDigits; } /** * Sets the minimum number of digits allowed in the fraction portion of a * number. minimumFractionDigits must be <= maximumFractionDigits. If the * new value for minimumFractionDigits exceeds the current value * of maximumFractionDigits, then maximumIntegerDigits will also be set to * the new value * @param newValue the minimum number of fraction digits to be shown; if * less than zero, then zero is used. The concrete subclass may enforce an * upper limit to this value appropriate to the numeric type being formatted. * @see #getMinimumFractionDigits */ public void setMinimumFractionDigits(int newValue) { minimumFractionDigits = Math.max(0,newValue); if (maximumFractionDigits < minimumFractionDigits) maximumFractionDigits = minimumFractionDigits; } /** * Gets the currency used by this number format when formatting * currency values. The initial value is derived in a locale dependent * way. The returned value may be null if no valid * currency could be determined and no currency has been set using * {@link #setCurrency(java.util.Currency) setCurrency}. * <p> * The default implementation throws * <code>UnsupportedOperationException</code>. * * @return the currency used by this number format, or <code>null</code> * @exception UnsupportedOperationException if the number format class * doesn't implement currency formatting * @since 1.4 */ public Currency getCurrency() { throw new UnsupportedOperationException(); } /** * Sets the currency used by this number format when formatting * currency values. This does not update the minimum or maximum * number of fraction digits used by the number format. * <p> * The default implementation throws * <code>UnsupportedOperationException</code>. * * @param currency the new currency to be used by this number format * @exception UnsupportedOperationException if the number format class * doesn't implement currency formatting * @exception NullPointerException if <code>currency</code> is null * @since 1.4 */ public void setCurrency(Currency currency) { throw new UnsupportedOperationException(); } // =======================privates=============================== private static NumberFormat getInstance(Locale desiredLocale, int choice) { /* try the cache first */ String[] numberPatterns = (String[])cachedLocaleData.get(desiredLocale); if (numberPatterns == null) { /* cache miss */ ResourceBundle resource = LocaleData.getLocaleElements(desiredLocale); numberPatterns = resource.getStringArray("NumberPatterns"); /* update cache */ cachedLocaleData.put(desiredLocale, numberPatterns); } DecimalFormatSymbols symbols = new DecimalFormatSymbols(desiredLocale); int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice; DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols); if (choice == INTEGERSTYLE) { format.setMaximumFractionDigits(0); format.setDecimalSeparatorAlwaysShown(false); format.setParseIntegerOnly(true); } else if (choice == CURRENCYSTYLE) { format.adjustForCurrencyDefaultFractionDigits(); } return format; } /** * First, read in the default serializable data. * * Then, if <code>serialVersionOnStream</code> is less than 1, indicating that
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -