⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 currencyconverter.java

📁 struts+hibernate开发的BBS源码
💻 JAVA
字号:
package com.bbs.util;import java.text.DecimalFormat;import java.text.ParseException;import org.apache.commons.beanutils.ConversionException;import org.apache.commons.beanutils.Converter;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * This class is converts a Double to a double-digit String * (and vise-versa) by BeanUtils when copying properties. * Registered for use in BaseAction. * * <p> * <a href="CurrencyConverter.java.html"><i>View Source</i></a> * </p> * * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> */public class CurrencyConverter implements Converter {    protected final Log log = LogFactory.getLog(CurrencyConverter.class);    protected final DecimalFormat formatter = new DecimalFormat("###,###.00");    /**     * Convert a String to a Double and a Double to a String     *     * @param type the class type to output     * @param value the object to convert     * @return object the converted object (Double or String)     */    public final Object convert(final Class type, final Object value) {        // for a null value, return null        if (value == null) {            return null;        } else {            if (value instanceof String) {                if (log.isDebugEnabled()) {                    log.debug("value (" + value + ") instance of String");                }                try {                    if (StringUtils.isBlank(String.valueOf(value))) {                        return null;                    }                    if (log.isDebugEnabled()) {                        log.debug("converting '" + value + "' to a decimal");                    }                    //formatter.setDecimalSeparatorAlwaysShown(true);                    Number num = formatter.parse(String.valueOf(value));                    return new Double(num.doubleValue());                } catch (ParseException pe) {                    pe.printStackTrace();                }            } else if (value instanceof Double) {                if (log.isDebugEnabled()) {                    log.debug("value (" + value + ") instance of Double");                    log.debug("returning double: " + formatter.format(value));                }                return formatter.format(value);            }        }        throw new ConversionException("Could not convert " + value + " to " +                                      type.getName() + "!");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -