org.apache.commons.beanutils.converters.BigDecimalConverter Java Examples

The following examples show how to use org.apache.commons.beanutils.converters.BigDecimalConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FieldHolder.java    From bluetooth-gatt-parser with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a BigInteger representation of the field or a default value in case if the field cannot
 * be converted to a BigInteger.
 * @param def the default value to be returned if an error occurs converting the field
 * @return a BigInteger representation of the field
 */
public BigInteger getBigInteger(BigInteger def) {
    BigDecimal result = new BigDecimalConverter(null).convert(BigDecimal.class, prepareValue());
    return result != null
            ? result.multiply(BigDecimal.valueOf(getMultiplier()))
                    .add(BigDecimal.valueOf(getOffset())).setScale(0, RoundingMode.HALF_UP).toBigInteger()
            : def;
}
 
Example #2
Source File: FieldHolder.java    From bluetooth-gatt-parser with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a BigDecimal representation of the field or a default value in case if the field cannot
 * be converted to a BigDecimal.
 * @param def the default value to be returned if an error occurs converting the field
 * @return a BigDecimal representation of the field
 */
public BigDecimal getBigDecimal(BigDecimal def) {
    BigDecimal result = new BigDecimalConverter(null).convert(BigDecimal.class, prepareValue());
    return result != null
            ? result.multiply(BigDecimal.valueOf(getMultiplier()))
            : def;
}
 
Example #3
Source File: ContextInitializer.java    From payment with Apache License 2.0 5 votes vote down vote up
public void initialize(ConfigurableApplicationContext applicationContext) {
    logger.debug("Context is initialized");
    BeanUtilsBean.getInstance().
            getConvertUtils().register(new LongConverter(null),
            Long.class);
    BeanUtilsBean.getInstance()
            .getConvertUtils().register(new BigDecimalConverter(null),
            BigDecimal.class);
}
 
Example #4
Source File: ConvertUtil.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Register standard default null.
 * 
 * @see ConvertUtilsBean#registerPrimitives(boolean) registerPrimitives(boolean throwException)
 * @see ConvertUtilsBean#registerStandard(boolean,boolean) registerStandard(boolean throwException, boolean defaultNull)
 * @see ConvertUtilsBean#registerOther(boolean) registerOther(boolean throwException)
 * @see ConvertUtilsBean#registerArrays(boolean,int) registerArrays(boolean throwException, int defaultArraySize)
 * @see ConvertUtilsBean#deregister(Class) ConvertUtilsBean.deregister(Class)
 * @since 1.11.2
 */
public static void registerStandardDefaultNull(){
    ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
    ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
    ConvertUtils.register(new BooleanConverter(null), Boolean.class);
    ConvertUtils.register(new ByteConverter(null), Byte.class);
    ConvertUtils.register(new CharacterConverter(null), Character.class);
    ConvertUtils.register(new DoubleConverter(null), Double.class);
    ConvertUtils.register(new FloatConverter(null), Float.class);
    ConvertUtils.register(new IntegerConverter(null), Integer.class);
    ConvertUtils.register(new LongConverter(null), Long.class);
    ConvertUtils.register(new ShortConverter(null), Short.class);
    ConvertUtils.register(new StringConverter(null), String.class);
}
 
Example #5
Source File: KualiActionServlet.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
    * <p>Initialize other global characteristics of the controller servlet.</p>
    * Overridden to remove the ConvertUtils.deregister() command that caused problems
    * with the concurrent data dictionary load.  (KULRNE-4405)
    *
    * @exception ServletException if we cannot initialize these resources
    */
   @Override
protected void initOther() throws ServletException {

       String value = null;
       value = getServletConfig().getInitParameter("config");
       if (value != null) {
           config = value;
       }

       // Backwards compatibility for form beans of Java wrapper classes
       // Set to true for strict Struts 1.0 compatibility
       value = getServletConfig().getInitParameter("convertNull");
       if ("true".equalsIgnoreCase(value)
           || "yes".equalsIgnoreCase(value)
           || "on".equalsIgnoreCase(value)
           || "y".equalsIgnoreCase(value)
           || "1".equalsIgnoreCase(value)) {

           convertNull = true;
       }

       if (convertNull) {
           ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
           ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
           ConvertUtils.register(new BooleanConverter(null), Boolean.class);
           ConvertUtils.register(new ByteConverter(null), Byte.class);
           ConvertUtils.register(new CharacterConverter(null), Character.class);
           ConvertUtils.register(new DoubleConverter(null), Double.class);
           ConvertUtils.register(new FloatConverter(null), Float.class);
           ConvertUtils.register(new IntegerConverter(null), Integer.class);
           ConvertUtils.register(new LongConverter(null), Long.class);
           ConvertUtils.register(new ShortConverter(null), Short.class);
       }

       // KULRICE-8176: KFS Notes/Attachments Tab Functionality for Note Text Error - Visible/Special characters, spaces, or tabs
       parameterEncoding = getServletConfig().getInitParameter("PARAMETER_ENCODING");
   }
 
Example #6
Source File: ConvertUtil.java    From feilong-core with Apache License 2.0 2 votes vote down vote up
/**
 * 将 <code>toBeConvertedValue</code> 转换成 {@link java.math.BigDecimal}.
 * 
 * <h3>示例:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * ConvertUtil.toBigDecimal(null)                     = null
 * ConvertUtil.toBigDecimal("aaaa")                   = null
 * ConvertUtil.toBigDecimal(8)                        = BigDecimal.valueOf(8)
 * ConvertUtil.toBigDecimal("8")                      = BigDecimal.valueOf(8)
 * ConvertUtil.toBigDecimal(new BigDecimal("8"))      = BigDecimal.valueOf(8)
 * </pre>
 * 
 * <p>
 * 如果传入的参数 <code>toBeConvertedValue</code> 是 <b>数组</b>,那么<b>取第一个元素</b>进行转换,参见 {@link AbstractConverter#convertArray(Object)} L227:
 * </p>
 * 
 * <pre class="code">
 * ConvertUtil.toBigDecimal(new String[] { "1", "2", "3" }) = BigDecimal.valueOf(1)
 * </pre>
 * 
 * <p>
 * 如果传入的参数 <code>toBeConvertedValue</code> 是 <b>集合</b>,那么<b>取第一个元素</b>进行转换,参见 {@link AbstractConverter#convertArray(Object)} Line234:
 * </p>
 * 
 * <pre class="code">
 * ConvertUtil.toBigDecimal(toList("1", "2")) = BigDecimal.valueOf(1)
 * </pre>
 * 
 * </blockquote>
 * 
 * <h3>{@link java.lang.Double} 转成 {@link java.math.BigDecimal}注意点:</h3>
 * 
 * <blockquote>
 * 
 * <p>
 * <span style="color:red">推荐使用 {@link BigDecimal#valueOf(double)}</span>,不建议使用 <code>new BigDecimal(double)</code>,参见 JDK API<br>
 * </p>
 * 
 * <ul>
 * <li>{@code new BigDecimal(0.1) ====> 0.1000000000000000055511151231257827021181583404541015625}</li>
 * <li>{@code BigDecimal.valueOf(0.1) ====> 0.1}</li>
 * </ul>
 * 
 * <p>
 * 本方法底层调用的是 {@link NumberConverter#toNumber(Class, Class, Number)
 * NumberConverter#toNumber(Class, Class, Number)},正确的处理了 {@link java.lang.Double} 转成 {@link java.math.BigDecimal}
 * </p>
 * </blockquote>
 * 
 * @param toBeConvertedValue
 *            值
 * @return 如果 <code>toBeConvertedValue</code> 是null,返回 null<br>
 *         如果传入的参数 <code>toBeConvertedValue</code> 是 <b>数组</b>,那么<b>取第一个元素</b>进行转换<br>
 *         如果传入的参数 <code>toBeConvertedValue</code> 是 <b>集合</b>,那么<b>取第一个元素</b>进行转换<br>
 *         如果找不到转换器或者转换的时候出现了异常,返回 null
 * @see #convert(Object, Class)
 * @see org.apache.commons.beanutils.converters.NumberConverter#toNumber(Class, Class, Number)
 * @see org.apache.commons.beanutils.converters.BigDecimalConverter
 */
public static BigDecimal toBigDecimal(Object toBeConvertedValue){
    return new BigDecimalConverter(null).convert(BigDecimal.class, toBeConvertedValue);
}