Example usage for java.lang Number floatValue

List of usage examples for java.lang Number floatValue

Introduction

In this page you can find the example usage for java.lang Number floatValue.

Prototype

public abstract float floatValue();

Source Link

Document

Returns the value of the specified number as a float .

Usage

From source file:net.solarnetwork.node.dao.jdbc.power.JdbcPowerDatumDao.java

@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public List<PowerDatum> getDatumNotUploaded(String destination) {
    return findDatumNotUploaded(new RowMapper<PowerDatum>() {

        @Override//w  w w  .  ja  v a2 s  .com
        public PowerDatum mapRow(ResultSet rs, int rowNum) throws SQLException {
            if (log.isTraceEnabled()) {
                log.trace("Handling result row " + rowNum);
            }
            PowerDatum datum = new PowerDatum();
            int col = 1;
            datum.setCreated(rs.getTimestamp(col++));
            datum.setSourceId(rs.getString(col++));

            Number val = (Number) rs.getObject(col++);
            datum.setLocationId(val == null ? null : val.longValue());

            val = (Number) rs.getObject(col++);
            datum.setWatts(val == null ? null : val.intValue());

            val = (Number) rs.getObject(col++);
            datum.setBatteryVolts(val == null ? null : val.floatValue());

            val = (Number) rs.getObject(col++);
            datum.setBatteryAmpHours(val == null ? null : val.doubleValue());

            val = (Number) rs.getObject(col++);
            datum.setDcOutputVolts(val == null ? null : val.floatValue());

            val = (Number) rs.getObject(col++);
            datum.setDcOutputAmps(val == null ? null : val.floatValue());

            val = (Number) rs.getObject(col++);
            datum.setAcOutputVolts(val == null ? null : val.floatValue());

            val = (Number) rs.getObject(col++);
            datum.setAcOutputAmps(val == null ? null : val.floatValue());

            val = (Number) rs.getObject(col++);
            datum.setWattHourReading(val == null ? null : val.longValue());

            val = (Number) rs.getObject(col++);
            datum.setAmpHourReading(val == null ? null : val.doubleValue());

            return datum;
        }
    });
}

From source file:moe.encode.airblock.commands.arguments.types.PrimitiveParser.java

@Override
public Object convert(Executor executor, ArgumentConverter parser, Type type, String value) {
    Class<?> cls = ReflectionUtils.toClass(type);
    if (ClassUtils.isPrimitiveWrapper(cls))
        cls = ClassUtils.wrapperToPrimitive(cls);

    if (cls.equals(boolean.class))
        return this.isTrue(executor, value);
    else if (cls.equals(char.class)) {
        if (value.length() > 0)
            throw new NumberFormatException("Character arguments cannot be longer than one characters");
        return value.charAt(0);
    }/*from  w  ww  .  j  av  a2 s. co m*/

    // Get the locale of the user and get a number-format according to it.
    LocaleResolver resolver = TranslationManager.getResolver(executor);
    Locale locale;
    if (resolver != null)
        locale = resolver.getLocale();
    else
        locale = Locale.ENGLISH;

    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    nf.setGroupingUsed(true);

    // Parse the value.
    Number result;
    try {
        result = nf.parse(value);
    } catch (ParseException e) {
        NumberFormatException nfe = new NumberFormatException("Invalid number");
        nfe.initCause(e);
        throw nfe;
    }

    // Returns the value in the correct type.
    if (cls.equals(int.class))
        return result.intValue();
    else if (cls.equals(float.class))
        return result.floatValue();
    else if (cls.equals(double.class))
        return result.doubleValue();
    else if (cls.equals(byte.class))
        return result.byteValue();
    else if (cls.equals(short.class))
        return result.shortValue();
    else if (cls.equals(long.class))
        return result.longValue();

    throw new NumberFormatException("Unknown primitive type.");
}

From source file:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 * @param number the number to convert//ww  w.  ja va2  s .c o m
 * @param targetClass the target class to convert to
 * @return the converted number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class<?> targetClass)
        throws IllegalArgumentException {

    //   Assert.notNull(number, "Number must not be null");
    //   Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Byte(number.byteValue());
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Short(number.shortValue());
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Integer(number.intValue());
    } else if (targetClass.equals(Long.class)) {
        return new Long(number.longValue());
    } else if (targetClass.equals(Float.class)) {
        return new Float(number.floatValue());
    } else if (targetClass.equals(Double.class)) {
        return new Double(number.doubleValue());
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:org.polymap.rhei.field.NumberValidator.java

public M transform2Model(String fieldValue) throws Exception {
    if (fieldValue == null) {
        return null;
    } else if (fieldValue instanceof String) {
        ParsePosition pp = new ParsePosition(0);
        Number result = nf.parse((String) fieldValue, pp);

        if (pp.getErrorIndex() > -1 || pp.getIndex() < ((String) fieldValue).length()) {
            throw new ParseException("field value: " + fieldValue, pp.getErrorIndex());
        }//from  w  w w.  j a v a2s .c  om

        log.debug("value: " + fieldValue + " -> " + result.doubleValue());

        // XXX check max digits

        if (Float.class.isAssignableFrom(targetClass)) {
            return (M) Float.valueOf(result.floatValue());
        } else if (Double.class.isAssignableFrom(targetClass)) {
            return (M) Double.valueOf(result.floatValue());
        } else if (Integer.class.isAssignableFrom(targetClass)) {
            return (M) Integer.valueOf(result.intValue());
        } else if (Long.class.isAssignableFrom(targetClass)) {
            return (M) Long.valueOf(result.longValue());
        } else {
            throw new RuntimeException("Unsupported target type: " + targetClass);
        }
    } else {
        throw new RuntimeException("Unhandled field value type: " + fieldValue);
    }
}

From source file:vteaexploration.plottools.panels.XYChartPanel.java

private double getRangeofData(ArrayList alVolumes, int x) {

    ListIterator litr = alVolumes.listIterator();

    ArrayList<Number> al = new ArrayList<Number>();

    Number low = 0;/*from  www  .j  a v  a  2 s.  c om*/
    Number high = 0;
    Number test;

    while (litr.hasNext()) {
        try {
            MicroObjectModel volume = (MicroObjectModel) litr.next();
            Number Corrected = processPosition(x, volume);
            al.add(Corrected);

            if (Corrected.floatValue() < low.floatValue()) {
                low = Corrected;
            }
            if (Corrected.floatValue() > high.floatValue()) {
                high = Corrected;
            }
        } catch (NullPointerException e) {
        }
    }

    return high.longValue() - low.longValue();

}

From source file:com.cypress.cysmart.BLEServiceFragments.CSCService.java

private void showCaloriesBurnt() {
    try {//from  ww w.  java 2s. c  om
        Number numWeight = NumberFormat.getInstance().parse(weightString);
        weightInt = numWeight.floatValue();
        float caloriesBurntInt = (((showElapsedTime()) * weightInt) * 8);
        caloriesBurntInt = caloriesBurntInt / 1000;
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumFractionDigits(4);
        formatter.setMaximumFractionDigits(4);
        String finalBurn = formatter.format(caloriesBurntInt);
        mCaloriesBurnt.setText(finalBurn);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:javadz.beanutils.locale.converters.FloatLocaleConverter.java

/**
 * Convert the specified locale-sensitive input object into an output object of the
 * specified type.  This method will return Float value or throw exception if value
 * can not be stored in the Float./*  w  w  w. j a v a  2 s  . c om*/
 *
 * @param value The input object to be converted
 * @param pattern The pattern is used for the convertion
 * @return The converted value
 *
 * @exception ConversionException if conversion cannot be performed
 *  successfully
 * @throws ParseException if an error occurs parsing a String to a Number
 */
protected Object parse(Object value, String pattern) throws ParseException {
    final Number parsed = (Number) super.parse(value, pattern);
    double doubleValue = parsed.doubleValue();
    double posDouble = (doubleValue >= (double) 0) ? doubleValue : (doubleValue * (double) -1);
    if (posDouble != 0 && (posDouble < Float.MIN_VALUE || posDouble > Float.MAX_VALUE)) {
        throw new ConversionException("Supplied number is not of type Float: " + parsed);
    }
    return new Float(parsed.floatValue()); // unlike superclass it returns Float type
}

From source file:com.sun.faces.el.impl.Coercions.java

/**
 * Coerces a Number to the given primitive number class
 *//* w w  w  .  j a  v  a  2s  .c o  m*/
static Number coerceToPrimitiveNumber(Number pValue, Class pClass) throws ElException {
    if (pClass == Byte.class || pClass == Byte.TYPE) {
        return PrimitiveObjects.getByte(pValue.byteValue());
    } else if (pClass == Short.class || pClass == Short.TYPE) {
        return PrimitiveObjects.getShort(pValue.shortValue());
    } else if (pClass == Integer.class || pClass == Integer.TYPE) {
        return PrimitiveObjects.getInteger(pValue.intValue());
    } else if (pClass == Long.class || pClass == Long.TYPE) {
        return PrimitiveObjects.getLong(pValue.longValue());
    } else if (pClass == Float.class || pClass == Float.TYPE) {
        return PrimitiveObjects.getFloat(pValue.floatValue());
    } else if (pClass == Double.class || pClass == Double.TYPE) {
        return PrimitiveObjects.getDouble(pValue.doubleValue());
    } else if (pClass == BigInteger.class) {
        if (pValue instanceof BigDecimal)
            return ((BigDecimal) pValue).toBigInteger();
        else
            return BigInteger.valueOf(pValue.longValue());
    } else if (pClass == BigDecimal.class) {
        if (pValue instanceof BigInteger)
            return new BigDecimal((BigInteger) pValue);
        else
            return new BigDecimal(pValue.doubleValue());
    } else {
        return PrimitiveObjects.getInteger(0);
    }
}

From source file:org.nd4j.linalg.api.complex.BaseComplexFloat.java

@Override
public IComplexNumber set(Number real, Number imag) {
    this.real = real.floatValue();
    this.imag = imag.floatValue();
    return this;
}

From source file:org.mrgeo.mapalgebra.parser.jexl.JexlParserAdapter.java

@Override
public Object evaluate(ParserNode node) throws ParserException {
    JexlNode nativeNode = (JexlNode) node.getNativeNode();
    if (nativeNode instanceof ASTAssignment) {
        JexlNode valueNode = nativeNode.jjtGetChild(1);
        return valueNode.jjtGetValue();
    } else if (nativeNode instanceof ASTNumberLiteral) {
        return ((ASTNumberLiteral) nativeNode).getLiteral();
    } else if (nativeNode instanceof ASTStringLiteral) {
        return ((ASTStringLiteral) nativeNode).getLiteral();
    } else if ((nativeNode instanceof ASTUnaryMinusNode)
            && (nativeNode.jjtGetChild(0) instanceof ASTNumberLiteral)) {
        Number oldNum = ((ASTNumberLiteral) nativeNode.jjtGetChild(0)).getLiteral();
        if (oldNum instanceof Integer) {
            return -oldNum.intValue();
        } else if (oldNum instanceof Double) {
            return -oldNum.doubleValue();
        } else if (oldNum instanceof Float) {
            return (float) (0.0 - oldNum.floatValue());
        } else if (oldNum instanceof Short) {
            return -oldNum.shortValue();
        } else if (oldNum instanceof Long) {
            return -oldNum.longValue();
        } else if (oldNum instanceof Byte) {
            return -oldNum.byteValue();
        }//  w  ww. j a va 2 s .c  o m
    }
    return ((JexlNode) node.getNativeNode()).jjtGetValue();
}