📄 utilformatout.java
字号:
/* * $Id: UtilFormatOut.java 7099 2006-03-28 23:02:54Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.base.util;import java.text.DateFormat;import java.text.DecimalFormat;import java.text.NumberFormat;import java.text.ParseException;import java.util.Locale;import java.util.Currency;/** * General output formatting functions - mainly for helping in JSPs * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7099 $ * @since 2.0 */public class UtilFormatOut { public static final String module = UtilFormatOut.class.getName(); public static String safeToString(Object obj) { if (obj != null) { return obj.toString(); } else { return ""; } } // ------------------- price format handlers ------------------- static DecimalFormat priceDecimalFormat = new DecimalFormat("#,##0.00"); static DecimalFormat priceNumberFormat = new DecimalFormat("##0.00"); /** Formats a Double representing a price into a string * @param price The price Double to be formatted * @return A String with the formatted price */ public static String formatPrice(Double price) { if (price == null) return ""; return formatPrice(price.doubleValue()); } /** Formats a double representing a price into a string * @param price The price double to be formatted * @return A String with the formatted price */ public static String formatPrice(double price) { return priceDecimalFormat.format(price); } public static Double formatPriceNumber(double price) { try { return new Double(priceDecimalFormat.parse(formatPrice(price)).doubleValue()); } catch (ParseException e) { Debug.logError(e, module); return new Double(price); } } /** Formats a double into a properly formatted currency string based on isoCode and Locale * @param price The price double to be formatted * @param isoCode the currency ISO code * @param locale The Locale used to format the number * @return A String with the formatted price */ public static String formatCurrency(double price, String isoCode, Locale locale) { //Debug.logInfo("formatting currency: " + price + ", isoCode: " + isoCode + ", locale: " + locale, module); com.ibm.icu.text.NumberFormat nf = com.ibm.icu.text.NumberFormat.getCurrencyInstance(locale); if (isoCode != null && isoCode.length() > 1) { nf.setCurrency(com.ibm.icu.util.Currency.getInstance(isoCode)); } else { Debug.logWarning("No isoCode specified to format currency value:" + price, module); } return nf.format(price); } /** Formats a double into a properly formatted currency string based on isoCode and Locale * @param price The price Double to be formatted * @param isoCode the currency ISO code * @param locale The Locale used to format the number * @return A String with the formatted price */ public static String formatCurrency(Double price, String isoCode, Locale locale) { return formatCurrency(price.doubleValue(), isoCode, locale); } /** Formats a Double into a properly spelled out number string based on Locale * @param amount The amount Double to be formatted * @param locale The Locale used to format the number * @return A String with the formatted number */ public static String formatSpelledOutAmount(Double amount, Locale locale) { return formatSpelledOutAmount(amount.doubleValue(), locale); } /** Formats a double into a properly spelled out number string based on Locale * @param amount The amount double to be formatted * @param locale The Locale used to format the number * @return A String with the formatted number */ public static String formatSpelledOutAmount(double amount, Locale locale) { //Debug.logInfo("formatting currency: " + price + ", isoCode: " + isoCode + ", locale: " + locale, module); com.ibm.icu.text.NumberFormat nf = new com.ibm.icu.text.RuleBasedNumberFormat(locale, com.ibm.icu.text.RuleBasedNumberFormat.SPELLOUT); return nf.format(amount); } /** Formats a double into a properly formatted string, with two decimals, based on Locale * @param amount The amount double to be formatted * @param locale The Locale used to format the number * @return A String with the formatted amount */ // This method should be used in place of formatPrice because it is locale aware. public static String formatAmount(double amount, Locale locale) { com.ibm.icu.text.NumberFormat nf = com.ibm.icu.text.NumberFormat.getInstance(locale); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); return nf.format(amount); } // ------------------- percentage format handlers ------------------- static DecimalFormat percentageDecimalFormat = new DecimalFormat("##0.##%"); /** Formats a Double representing a percentage into a string * @param percentage The percentage Double to be formatted * @return A String with the formatted percentage */ public static String formatPercentage(Double percentage) { if (percentage == null) return ""; return formatPercentage(percentage.doubleValue()); } /** Formats a double representing a percentage into a string * @param percentage The percentage double to be formatted * @return A String with the formatted percentage */ public static String formatPercentage(double percentage) { return percentageDecimalFormat.format(percentage); } // ------------------- quantity format handlers ------------------- static DecimalFormat quantityDecimalFormat = new DecimalFormat("#,##0.###"); /** Formats an Long representing a quantity into a string * @param quantity The quantity Long to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(Long quantity) { if (quantity == null) return ""; else return formatQuantity(quantity.doubleValue()); } /** Formats an int representing a quantity into a string * @param quantity The quantity long to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(long quantity) { return formatQuantity((double) quantity); } /** Formats an Integer representing a quantity into a string * @param quantity The quantity Integer to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(Integer quantity) { if (quantity == null) return ""; else return formatQuantity(quantity.doubleValue()); } /** Formats an int representing a quantity into a string * @param quantity The quantity int to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(int quantity) { return formatQuantity((double) quantity); } /** Formats a Float representing a quantity into a string * @param quantity The quantity Float to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(Float quantity) { if (quantity == null) return ""; else return formatQuantity(quantity.doubleValue()); } /** Formats a float representing a quantity into a string * @param quantity The quantity float to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(float quantity) { return formatQuantity((double) quantity); } /** Formats an Double representing a quantity into a string * @param quantity The quantity Double to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(Double quantity) { if (quantity == null) return ""; else return formatQuantity(quantity.doubleValue()); } /** Formats an double representing a quantity into a string * @param quantity The quantity double to be formatted * @return A String with the formatted quantity */ public static String formatQuantity(double quantity) { return quantityDecimalFormat.format(quantity); } public static String formatPaddedNumber(long number, int numericPadding) { StringBuffer outStrBfr = new StringBuffer(Long.toString(number)); while (numericPadding > outStrBfr.length()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -