📄 format.java
字号:
/* * Core - Library of useful classes that are used in many CyberDemia projects. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.util;import java.text.NumberFormat;import java.text.DateFormat;import java.util.Locale;import java.util.Date;/*** Format is an utility class to perform locale-sensitive * formatting for various format types like currency, percent, date, etc.* This class cannot be instantiated. It is not thread-safe.** @author Alexander Yap* @version $Revision: 1.2 $ at $Date: 2004/02/18 08:35:17 $ by $Author: alexycyap $*/public abstract class Format{ /** * Current locale. Defaults to system locale. */ private static Locale s_locale = Locale.getDefault(); /** * Default currency formatter. This is cached here * to avoid fetching locale info multiple times. */ private static NumberFormat s_defCurrencyFormatter = NumberFormat.getCurrencyInstance(); /** * Customizable currency formatter. This is cached here * to avoid fetching locale info multiple times. */ private static NumberFormat s_customCurrencyFormatter = NumberFormat.getCurrencyInstance(); /** * Customizable percent formatter. This is cached here * to avoid fetching locale info multiple times. */ private static NumberFormat s_customPercentFormatter = NumberFormat.getPercentInstance(); /** * Customizable number formatter. This is cached here * to avoid fetching locale info multiple times. */ private static NumberFormat s_customNumberFormatter = NumberFormat.getNumberInstance(); /** * Sets the current locale that is used to perform formatting. * This method should not be called too often because it causes retrieval * of locale info and creation of formatters each time it is invoked. * * @param loc Locale to use, or null to reset to system default. */ public static final void setLocale( Locale loc ) { if (loc == null) s_locale = Locale.getDefault(); else s_locale = loc; // Update the cached formatters for the new locale s_defCurrencyFormatter = NumberFormat.getCurrencyInstance(s_locale); s_customNumberFormatter = NumberFormat.getNumberInstance(s_locale); s_customCurrencyFormatter = NumberFormat.getCurrencyInstance(s_locale); s_customPercentFormatter = NumberFormat.getPercentInstance(s_locale); } /** * Gets the current locale. * If <code>setLocale(Locale)</code> has never been called, this method * will return the system default locale. */ public static final Locale getLocale() { return s_locale; } /** * Formats double value to currency String for the current locale, * using default NumberFormat settings. This is appropriate for most * currency display. For example, this rounds to 2 fractional digits * for English(US) currency, and applies grouping of 3. * @param val value to format * @return Currency string for the current locale. */ public static final String formatCurrency(double val) { if (Double.isInfinite(val)) { return "Infinite"; } else if (Double.isNaN(val)) { return "NaN"; } // A separate formatter with fixed settings is used for efficiency // because most currency formatting is the same. return s_defCurrencyFormatter.format(val); } /** * Formats double value to currency String for the current locale, * using NumberFormat. The number of fractional digits can be specified. * Grouping can be enabled or disabled. * This method is probably only useful in small number of cases where * it is required to round to the nearest monetary unit, e.g. dollar. * @param val value to format * @param minFracDigits minimum number of fractional digits * @param maxFracDigits maximum number of fractional digits * @param grouped enable or disable grouping * @return Currency string for the current locale. */ public static final String formatCurrency(double val, int minFracDigits, int maxFracDigits, boolean grouped) { if (Double.isInfinite(val)) { return "Infinite"; } else if (Double.isNaN(val)) { return "NaN"; } s_customCurrencyFormatter.setMinimumFractionDigits(minFracDigits); s_customCurrencyFormatter.setMaximumFractionDigits(maxFracDigits); s_customCurrencyFormatter.setGroupingUsed( grouped); return s_customCurrencyFormatter.format(val); } /** * Formats double value to percent String for the current locale, * using NumberFormat. The number of fractional digits can be specified. * Grouping can be enabled or disabled. * Note: 1 is formatted to 100%, 0.5 to 50% , 20 to 2000% etc * * @param val value to format * @param minFracDigits minimum number of fractional digits * @param maxFracDigits maximum number of fractional digits * @param grouped enable or disable grouping * @return Percentage string for the current locale. */ public static final String formatPercent(double val, int minFracDigits, int maxFracDigits, boolean grouped) { if (Double.isInfinite(val)) { return "Infinite"; } else if (Double.isNaN(val)) { return "NaN"; } s_customPercentFormatter.setMinimumFractionDigits(minFracDigits); s_customPercentFormatter.setMaximumFractionDigits(maxFracDigits); s_customPercentFormatter.setGroupingUsed(grouped); return s_customPercentFormatter.format(val); } /** * Formats double value to numeric display String for the current locale, * using NumberFormat. The number of fractional digits can be specified. * Grouping can be enabled or disabled. * @param val value to format * @param minFracDigits minimum number of fractional digits * @param maxFracDigits maximum number of fractional digits * @param grouped enable or disable grouping * @return Numeric display string for the current locale. */ public static final String formatNumber(double val, int minFracDigits, int maxFracDigits, boolean grouped) { if (Double.isInfinite(val)) { return "Infinite"; } else if (Double.isNaN(val)) { return "NaN"; } s_customNumberFormatter.setMinimumFractionDigits(minFracDigits); s_customNumberFormatter.setMaximumFractionDigits(maxFracDigits); s_customNumberFormatter.setGroupingUsed(grouped); return s_customNumberFormatter.format(val); } /** * Formats a Date to a display String for the current locale, * using DateFormat. The formatting style can be specified. * @param dt Date to format * @param style Style to format the Date with, as specified in <code>java.text.DateFormat</code>. * @return Date string for the current locale. */ public static final String formatDate(Date dt, int style) { return DateFormat.getDateInstance(style,s_locale).format(dt); } /** * Test program for this utility class. * Should be updated to test any new features. */ static public void main(String[] args) { double[] nums= { 0.321432, -0.6543465, 321.6, -543265 }; Locale[] locs = { Locale.getDefault(), Locale.US, Locale.UK, Locale.FRANCE, Locale.GERMANY }; Date today = new Date(); for (int lidx=0; lidx<locs.length; lidx++) { System.out.println("\n===========================================================\n"); setLocale( locs[lidx] ); System.out.println("Testing locale : "+locs[lidx].getDisplayName()); System.out.println("Today's date (DEFAULT style) : "+formatDate(today, DateFormat.DEFAULT )); for (int nidx=0; nidx<nums.length; nidx++) { System.out.println("----------------------------------------------------------"); System.out.println("Number "+ String.valueOf(nums[nidx])); System.out.println("Number formatter : "+formatNumber(nums[nidx],0,3,true)); System.out.println("Percent formatter : "+formatPercent(nums[nidx],0,3,true)); System.out.println("Currency formatter : "+formatCurrency(nums[nidx])); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -