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

📄 bigdecimalconverter.java

📁 一个关于tlms的一个小程序 看看能否帮助到别人
💻 JAVA
字号:
/**
 * ============================================= 
 * Copyright 2006 szmx
 *
 * Change Revision
 * --------------------------------
 *   Date                Author         Remarks
 *   2006-4-20            ozhang     Create com.szmx.framework.util.convert.BigDecimalConverter
 * =============================================
 */

package com.szmx.framework.util.convert;

import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.ConversionException;

import java.math.BigDecimal;

public class BigDecimalConverter implements Converter {


    // ----------------------------------------------------------- Constructors


    /**
     * Create a {@link Converter} that will throw a {@link org.apache.commons.beanutils.ConversionException}
     * if a conversion error occurs.
     */
    public BigDecimalConverter() {

        this.defaultValue = null;
        this.useDefault = false;

    }


    /**
     * Create a {@link Converter} that will return the specified default value
     * if a conversion error occurs.
     *
     * @param defaultValue The default value to be returned
     */
    public BigDecimalConverter(Object defaultValue) {

        this.defaultValue = defaultValue;
        this.useDefault = false;

    }


    // ----------------------------------------------------- Instance Variables


    /**
     * The default value specified to our Constructor, if any.
     */
    private Object defaultValue = null;


    /**
     * Should we return the default value on conversion errors?
     */
    private boolean useDefault = false;


    // --------------------------------------------------------- Public Methods


    /**
     * Convert the specified input object into an output object of the
     * specified type.
     *
     * @param type Data type to which this value should be converted
     * @param value The input value to be converted
     *
     * @exception org.apache.commons.beanutils.ConversionException if conversion cannot be performed
     *  successfully
     */
    public Object convert(Class type, Object value) {

        if (value == null||"".equals(value)) {
//            if (useDefault) {
                return (defaultValue);
//            } else {
//                throw new ConversionException("No value specified");
//            }
        }

        if (value instanceof BigDecimal) {
            return (value);
        }

        try {
            return (new BigDecimal(value.toString()));
        } catch (Exception e) {
            if (useDefault) {
                return (defaultValue);
            } else {
                throw new ConversionException(e);
            }
        }

    }


}

⌨️ 快捷键说明

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