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:com.alibaba.wasp.plan.parser.druid.DruidParser.java

/**
 * The abstract class Number is the superclass of classes BigDecimal,
 * BigInteger, Byte, Double, Float, Integer, Long, and Short. Subclasses of
 * Number must provide methods to convert the represented numeric value to
 * byte, double, float, int, long, and short.
 *//*from  w  w  w .  j av  a2  s. c  om*/
public static byte[] convert(DataType type, Number number) throws UnsupportedException {
    // BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.
    if (number instanceof BigDecimal) {
        if (type == DataType.FLOAT) {
            return Bytes.toBytes(number.floatValue());
        } else if (type == DataType.DOUBLE) {
            return Bytes.toBytes(number.doubleValue());
        } else if (type == DataType.INT32) {
            return Bytes.toBytes(number.intValue());
        } else if (type == DataType.INT64) {
            return Bytes.toBytes(number.longValue());
        }
        return Bytes.toBytes((BigDecimal) number);
    } else if (number instanceof BigInteger) {
        throw new UnsupportedException(" BigInteger " + number + " Unsupported");
    } else if (number instanceof Byte) {
        return Bytes.toBytes((Byte) number);
    } else if (number instanceof Double) {
        double value = number.doubleValue();
        if (type == DataType.FLOAT) {
            return Bytes.toBytes((float) value);
        } else if (type == DataType.DOUBLE) {
            return Bytes.toBytes(value);
        } else if (type == DataType.INT32) {
            int iv = (int) value;
            return Bytes.toBytes(iv);
        } else if (type == DataType.INT64) {
            long lv = (long) value;
            return Bytes.toBytes(lv);
        }
    } else if (number instanceof Float) {
        float value = number.floatValue();
        if (type == DataType.FLOAT) {
            return Bytes.toBytes(value);
        } else if (type == DataType.DOUBLE) {
            return Bytes.toBytes((float) value);
        } else if (type == DataType.INT32) {
            int iv = (int) value;
            return Bytes.toBytes(iv);
        } else if (type == DataType.INT64) {
            long lv = (long) value;
            return Bytes.toBytes(lv);
        }
    } else if (number instanceof Integer) {
        int value = number.intValue();
        if (type == DataType.INT32) {
            return Bytes.toBytes((Integer) value);
        } else if (type == DataType.INT64) {
            return Bytes.toBytes((long) value);
        } else if (type == DataType.FLOAT) {
            float fv = (float) value;
            return Bytes.toBytes(fv);
        } else if (type == DataType.DOUBLE) {
            double fv = (double) value;
            return Bytes.toBytes(fv);
        }
    } else if (number instanceof Long) {
        long value = number.longValue();
        if (type == DataType.INT32) {
            return Bytes.toBytes((int) value);
        } else if (type == DataType.INT64) {
            return Bytes.toBytes(value);
        } else if (type == DataType.FLOAT) {
            float fv = (float) value;
            return Bytes.toBytes(fv);
        } else if (type == DataType.DOUBLE) {
            double fv = (double) value;
            return Bytes.toBytes(fv);
        }
    } else if (number instanceof Short) {
        return Bytes.toBytes((Short) number);
    }
    throw new UnsupportedException("Unknown Number:" + number + " Type:" + type + " Unsupported ");
}

From source file:org.caleydo.view.bicluster.elem.GLRootElement.java

private static List<Integer> findBestColumns(Integer col, TablePerspective t) {
    final VirtualArray va = t.getRecordPerspective().getVirtualArray();
    List<IntFloat> tmp = new ArrayList<>(va.size());
    final int max = MyPreferences.getMaxNumberofGOs();
    final float pMax = MyPreferences.getMaximalGOPValue();
    final Table table = t.getDataDomain().getTable();
    for (Integer rec : va) {
        final Number n = (Number) table.getRaw(col, rec);
        if (n == null)
            continue;
        float p = n.floatValue();
        if (p > pMax || Float.isNaN(p))
            continue;
        tmp.add(new IntFloat(rec, p));
    }/*w w w.j  a v  a 2  s  . com*/
    Collections.sort(tmp, IntFloat.BY_MEMBERSHIP);
    List<Integer> top = Lists.transform(tmp, IntFloat.TO_INDEX);
    if (max < tmp.size())
        return top.subList(0, max);
    return top;
}

From source file:com.jeeframework.util.validate.GenericTypeValidator.java

/**
 *  Checks if the value can safely be converted to a float primitive.
 *
 *@param  value   The value validation is being performed on.
 *@param  locale  The locale to use to parse the number (system default if
 *      null)/*from ww  w. j  a  va 2  s . com*/
 *@return the converted Float value.
 */
public static Float formatFloat(String value, Locale locale) {
    Float result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error      and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Float.MAX_VALUE * -1) && num.doubleValue() <= Float.MAX_VALUE) {
                result = new Float(num.floatValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a float primitive.
 * /*from   ww  w. j  av a  2 s. c o  m*/
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Float value.
 */
public static Float formatFloat(String value, Locale locale) {
    Float result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Float.MAX_VALUE * -1) && num.doubleValue() <= Float.MAX_VALUE) {
                result = new Float(num.floatValue());
            }
        }
    }

    return result;
}

From source file:Main.java

/**
 * Divides num1 by num2, and return the result in the correct number class.
 * //  ww  w .  ja  va2s .  co m
 * @param num1 numerator
 * @param num2 denominator
 * @return num1/num2 in the most appropriate class
 */
static Number divide(Number num1, Number num2) {
    Number[] both = new Number[2];
    both[0] = num1;
    both[1] = num2;

    Number division = (Number) getObject(both);

    if (division == null) {
        return null;
    }

    //Integer, Long, Float, Double
    if (division instanceof Integer) {
        //we've got 2 integers, but we're going to use double anyways
        return new Double(num1.doubleValue() / num2.doubleValue());
    } else if (division instanceof Long) {
        return new Long(num1.longValue() / num2.longValue());
    } else if (division instanceof Float) {
        return new Float(num1.floatValue() / num2.floatValue());
    } else if (division instanceof Double) {
        return new Double(num1.doubleValue() / num2.doubleValue());
    } else {
        return null;
    }
}

From source file:org.paxle.se.index.lucene.impl.Converter.java

private static Fieldable number2field(org.paxle.core.doc.Field<?> field, Number data) {
    /* ===========================================================
     * Numbers/*from  w  w w  .ja v a  2s  .  co m*/
     * - may be stored (if so, not compressed)
     * - may be indexed (no tokenization)
     * - no term vectors
     * =========================================================== */
    final long num;
    if (data instanceof Double) {
        num = Double.doubleToLongBits(data.doubleValue());
    } else if (data instanceof Float) {
        num = Float.floatToIntBits(data.floatValue());
    } else {
        num = data.longValue();
    }

    if (field.isIndex()) {
        return new Field(field.getName(), PaxleNumberTools.longToString(num), store(field, false),
                index(field));
    } else {
        return new Field(field.getName(), PaxleNumberTools.toBytes(num), store(field, false));
    }
}

From source file:com.helpinput.core.Utils.java

public static boolean hasLength(final Number value) {
    return (value != null && value.floatValue() > 0);
}

From source file:org.kalypso.observation.result.TupleResultUtilities.java

public static void setNumberValue(final IRecord record, final IComponent component, final Number value) {
    final QName qname = component.getValueTypeName();
    if (XmlTypes.XS_DECIMAL.equals(qname))
        record.setValue(component, BigDecimal.valueOf(value.doubleValue()));
    else if (XmlTypes.XS_DOUBLE.equals(qname))
        record.setValue(component, Double.valueOf(value.doubleValue()));
    else if (XmlTypes.XS_FLOAT.equals(qname))
        record.setValue(component, Float.valueOf(value.floatValue()));
    else if (XmlTypes.XS_INT.equals(qname))
        record.setValue(component, Integer.valueOf(value.intValue()));
    else if (XmlTypes.XS_INTEGER.equals(qname))
        record.setValue(component, BigInteger.valueOf(value.longValue()));
    else if (XmlTypes.XS_LONG.equals(qname))
        record.setValue(component, Long.valueOf(value.longValue()));
    else if (XmlTypes.XS_SHORT.equals(qname))
        record.setValue(component, Short.valueOf(value.shortValue()));
    else/* ww  w. ja  v a 2s. co m*/
        throw new UnsupportedOperationException();

}

From source file:org.batoo.jpa.common.reflect.ReflectHelper.java

/**
 * Converts the number into number Type//from w  w  w. jav  a  2 s  .  com
 * 
 * @param value
 *            the number value
 * @param numberType
 *            the number type
 * @return the converted number value
 * 
 * @since $version
 * @author hceylan
 */
public static Number convertNumber(Number value, Class<?> numberType) {
    if (numberType == Integer.class) {
        return value.intValue();
    }

    if (numberType == Long.class) {
        return value.longValue();
    }

    if (numberType == Short.class) {
        return value.shortValue();
    }

    if (numberType == Byte.class) {
        return value.byteValue();
    }

    if (numberType == Float.class) {
        return value.floatValue();
    }

    if (numberType == Double.class) {
        return value.doubleValue();
    }

    throw new IllegalArgumentException(numberType + " not supported");
}

From source file:Main.java

/**
 * This method converts a given number into a target class. This method does not change the value (except when
 * explicitly casting to a more general type, e.g. from double to int), just the internal type representation. While
 * this is unnecessary while using normal java code, reflection based access to method parameters is a bit more
 * difficult. As far as possible, this method will prevent the ArgumentMismatch error when passing numbers as
 * parameters.//from ww  w  .  jav a2s. com
 * <p/>
 * If the value can not be converted to the given target class, it will be returned unchanged.
 *
 * @param targetClass Class to which the number should be converted, if possible.
 * @param value       Number value to convert.
 * @return 'value' converted to an instance of 'targetClass'.
 */
public static Object convertNumber(final Class targetClass, final Number value) {
    if (targetClass.equals(Double.class) || targetClass.equals(Double.TYPE))
        return value.doubleValue();
    if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE))
        return value.intValue();
    if (targetClass.equals(Long.class) || targetClass.equals(Long.TYPE))
        return value.longValue();
    if (targetClass.equals(Short.class) || targetClass.equals(Short.TYPE))
        return value.shortValue();
    if (targetClass.equals(Byte.class) || targetClass.equals(Byte.TYPE))
        return value.byteValue();
    if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE))
        return value.intValue();
    if (targetClass.equals(Float.class) || targetClass.equals(Float.TYPE))
        return value.floatValue();
    return value;
}