All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jodd.typeconverter.impl.FloatConverter Maven / Gradle / Ivy

// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.typeconverter.impl;

import jodd.typeconverter.TypeConversionException;
import jodd.typeconverter.TypeConverter;
import jodd.util.StringUtil;

/**
 * Converts given object to Float.
 * Conversion rules:
 * 
    *
  • null value is returned as null
  • *
  • object of destination type is simply casted
  • *
  • object is converted to string, trimmed, and then converted if possible.
  • *
* Number string may start with plus and minus sign. */ public class FloatConverter implements TypeConverter { public Float convert(Object value) { if (value == null) { return null; } if (value.getClass() == Float.class) { return (Float) value; } if (value instanceof Number) { return Float.valueOf(((Number)value).floatValue()); } if (value instanceof Boolean) { return ((Boolean) value).booleanValue() ? Float.valueOf(1) : Float.valueOf(0); } try { String stringValue = value.toString().trim(); if (StringUtil.startsWithChar(stringValue, '+')) { stringValue = stringValue.substring(1); } return Float.valueOf(stringValue); } catch (NumberFormatException nfex) { throw new TypeConversionException(value, nfex); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy