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:org.briljantframework.data.vector.Convert.java

@SuppressWarnings("unchecked")
private static <T> T convertNumber(Class<T> cls, Number num) {
    if (cls.equals(Double.class) || cls.equals(Double.TYPE)) {
        return (T) (Double) num.doubleValue();
    } else if (cls.equals(Float.class) || cls.equals(Float.TYPE)) {
        return (T) (Float) num.floatValue();
    } else if (cls.equals(Long.class) || cls.equals(Long.TYPE)) {
        return (T) (Long) num.longValue();
    } else if (cls.equals(Integer.class) || cls.equals(Integer.TYPE)) {
        return (T) (Integer) num.intValue();
    } else if (cls.equals(Short.class) || cls.equals(Short.TYPE)) {
        return (T) (Short) num.shortValue();
    } else if (cls.equals(Byte.class) || cls.equals(Byte.TYPE)) {
        return (T) (Byte) num.byteValue();
    } else if (Complex.class.equals(cls)) {
        return cls.cast(Complex.valueOf(num.doubleValue()));
    } else if (Logical.class.equals(cls)) {
        return cls.cast(num.intValue() == 1 ? Logical.TRUE : Logical.FALSE);
    } else if (Boolean.class.equals(cls)) {
        return cls.cast(num.intValue() == 1);
    } else {/*  w w  w  .  j a v a2 s  .  com*/
        return Na.of(cls);
    }
}

From source file:org.fhcrc.cpl.viewer.mrm.Utils.java

public static float[][] PDStoArray(PlotDataSupplier pds) {
    float retval[][] = null;
    if (pds == null)
        return retval;
    XYSeries xys = pds.getGraphData();/*from  w  ww.  j a  v a2s. c om*/
    int itemCount = xys.getItemCount();
    retval = new float[2][itemCount];
    for (int i = 0; i < itemCount; i++) {
        retval[0][i] = xys.getX(i).floatValue();
        Number y = xys.getY(i);
        if (y != null) {
            retval[1][i] = y.floatValue();
        } else {
            retval[1][i] = Float.NaN;
        }
    }
    return retval;
}

From source file:NumberUtils.java

/**
 * Converts the given number to a <code>Float</code> (by using <code>floatValue()</code>).
 *
 * @param number// w  ww.  j av  a2s  .  co  m
 * @return java.lang.Float
 */
public static Float toFloat(Number number) {
    return number == null || number instanceof Float ? (Float) number : new Float(number.floatValue());
}

From source file:org.saiku.reporting.core.builder.ReportBuilderUtil.java

public static void setupDefaultGrid(Band band, Element detailElement) {
    setupDefaultPadding(band, detailElement);

    final ElementStyleSheet styleSheet = detailElement.getStyle();

    // Always make the height of the detailElement dynamic to the band
    // According to thomas negative numbers equate to percentages
    styleSheet.setStyleProperty(ElementStyleKeys.MIN_HEIGHT, new Float(-100));

    final Object maybeBorderStyle = band.getAttribute(AttributeNames.Wizard.NAMESPACE,
            AttributeNames.Wizard.GRID_STYLE);
    final Object maybeBorderWidth = band.getAttribute(AttributeNames.Wizard.NAMESPACE,
            AttributeNames.Wizard.GRID_WIDTH);
    final Object maybeBorderColor = band.getAttribute(AttributeNames.Wizard.NAMESPACE,
            AttributeNames.Wizard.GRID_COLOR);

    if (!(maybeBorderColor instanceof Color && maybeBorderStyle instanceof BorderStyle
            && maybeBorderWidth instanceof Number)) {
        return;/*from w w w .  j  a  va2s. c o m*/
    }

    final BorderStyle style = (BorderStyle) maybeBorderStyle;
    final Color color = (Color) maybeBorderColor;
    final Number number = (Number) maybeBorderWidth;
    final Float width = new Float(number.floatValue());

    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_TOP_WIDTH, width);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_TOP_COLOR, color);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_TOP_STYLE, style);

    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_LEFT_WIDTH, width);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_LEFT_COLOR, color);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_LEFT_STYLE, style);

    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_BOTTOM_WIDTH, width);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_BOTTOM_COLOR, color);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_BOTTOM_STYLE, style);

    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_RIGHT_WIDTH, width);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_RIGHT_COLOR, color);
    styleSheet.setStyleProperty(ElementStyleKeys.BORDER_RIGHT_STYLE, style);
}

From source file:org.pentaho.reporting.engine.classic.core.layout.output.RenderUtility.java

public static float getEncoderQuality(final ReportAttributeMap attributeMap) {
    final Object attribute = attributeMap.getAttribute(AttributeNames.Core.NAMESPACE,
            AttributeNames.Core.IMAGE_ENCODING_QUALITY);
    if (attribute == null) {
        return 0.9f;
    }/* www .  j  av  a  2 s.  com*/

    if (attribute instanceof Number) {
        final Number n = (Number) attribute;
        final float v = n.floatValue();
        if (v < 0.01) {
            return 0.01f;
        }
        if (v > 0.999) {
            return 0.999f;
        }
        return v;
    }
    return 0.9f;
}

From source file:LineGraphDrawable.java

/**
 * Computes the scale factor to scale the given numeric data into the target
 * height./*  w  w w .j  a  v a2 s  . c o m*/
 * 
 * @param data
 *          the numeric data.
 * @param height
 *          the target height of the graph.
 * @return the scale factor.
 */
public static float getDivisor(final Number[] data, final int height) {
    if (data == null) {
        throw new NullPointerException("Data array must not be null.");
    }

    if (height < 1) {
        throw new IndexOutOfBoundsException("Height must be greater or equal to 1");
    }

    float max = Float.MIN_VALUE;
    float min = Float.MAX_VALUE;

    for (int index = 0; index < data.length; index++) {
        Number i = data[index];
        if (i == null) {
            continue;
        }

        final float numValue = i.floatValue();
        if (numValue < min) {
            min = numValue;
        }
        if (numValue > max) {
            max = numValue;
        }
    }

    if (max <= min) {
        return 1.0f;
    }
    if (height == 1) {
        return 0;
    }
    return (max - min) / (height - 1);
}

From source file:org.totschnig.myexpenses.Utils.java

/**
 * <a href="http://www.ibm.com/developerworks/java/library/j-numberformat/">http://www.ibm.com/developerworks/java/library/j-numberformat/</a>
 * @param strFloat parsed as float with the number format defined in the locale
 * @return the float retrieved from the string or null if parse did not succeed
 *//* ww w. j a v a 2  s.  c o m*/
public static Float validateNumber(String strFloat) {
    ParsePosition pp;
    NumberFormat nfDLocal = NumberFormat.getNumberInstance();
    nfDLocal.setGroupingUsed(false);
    pp = new ParsePosition(0);
    pp.setIndex(0);
    Number n = nfDLocal.parse(strFloat, pp);
    if (strFloat.length() != pp.getIndex() || n == null) {
        return null;
    } else {
        return n.floatValue();
    }
}

From source file:org.apache.accumulo.examples.wikisearch.function.QueryFunctions.java

public static Number abs(String fieldValue) {
    Number retval = null;/*  www  .j a  va  2s . com*/
    try {
        Number value = NumberUtils.createNumber(fieldValue);
        if (null == value)
            retval = (Number) Integer.MIN_VALUE;
        else if (value instanceof Long)
            retval = Math.abs(value.longValue());
        else if (value instanceof Double)
            retval = Math.abs(value.doubleValue());
        else if (value instanceof Float)
            retval = Math.abs(value.floatValue());
        else if (value instanceof Integer)
            retval = Math.abs(value.intValue());
    } catch (NumberFormatException nfe) {
        return (Number) Integer.MIN_VALUE;
    }
    return retval;
}

From source file:com.github.drbookings.DrBookingsApplication.java

static void readProperties() {
    final Properties prop = new Properties();
    InputStream input = null;/* ww  w.  ja  va 2  s .  co  m*/
    try {
        try {
            input = new FileInputStream(System.getProperty("user.home") + File.separator + CONFIG_FILE_PATH);
            if (logger.isInfoEnabled()) {
                logger.info("Reading properties from " + System.getProperty("user.home") + File.separator
                        + CONFIG_FILE_PATH);
            }
            prop.load(input);
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to read properties from " + System.getProperty("user.home")
                        + File.separator + CONFIG_FILE_PATH);
            }
        }
        try {
            SettingsManager.getInstance().setDataFile(new File(prop.getProperty(DATA_FILE_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + DATA_FILE_KEY + ", " + ex.toString());
            }
        }
        try {
            final Number n = Scripting.evaluateExpression(prop.getProperty(ADDITIONAL_COSTS_KEY));
            SettingsManager.getInstance().setAdditionalCosts(n.floatValue());
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + ADDITIONAL_COSTS_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance().setReferenceColdRentLongTerm(
                    Float.parseFloat(prop.getProperty(REFERENCE_COLD_RENT_LONGTERM_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + REFERENCE_COLD_RENT_LONGTERM_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance()
                    .setNumberOfRooms(Integer.parseInt(prop.getProperty(NUMBER_OF_ROOMS_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + NUMBER_OF_ROOMS_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance()
                    .setWorkHoursPerMonth(Float.parseFloat(prop.getProperty(WORK_HOURS_PER_MONTH_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + WORK_HOURS_PER_MONTH_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance().setRoomNamePrefix(prop.getProperty(ROOM_NAME_PREFIX_KEY));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + ROOM_NAME_PREFIX_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance()
                    .setShowNetEarnings(Boolean.parseBoolean(prop.getProperty(SHOW_NET_EARNINGS_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + SHOW_NET_EARNINGS_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance()
                    .setCleaningFees(Float.parseFloat(prop.getProperty(CLEANING_FEES_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + CLEANING_FEES_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance()
                    .setCleaningCosts(Float.parseFloat(prop.getProperty(CLEANING_COSTS_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + CLEANING_COSTS_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance().setServiceFees(Float.parseFloat(prop.getProperty(SERVICE_FEES_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + SERVICE_FEES_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance()
                    .setServiceFeesPercent(Float.parseFloat(prop.getProperty(SERVICE_FEES_PERCENT_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + SERVICE_FEES_PERCENT_KEY + ", " + ex.toString());
            }
        }
        try {
            SettingsManager.getInstance()
                    .setEarningsPayoutPercent(Float.parseFloat(prop.getProperty(EARNINGS_PAYOUT_PERCENT_KEY)));
        } catch (final Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to parse " + EARNINGS_PAYOUT_PERCENT_KEY + ", " + ex.toString());
            }
        }
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:Main.java

/**
 * Casts an object to the specified type
 *
 * @param var//from  www  .j ava 2 s .c om
 * @param type
 *
 */
static Object convert(Object var, Class type) {
    if (var instanceof Number) { //use number conversion

        Number newNum = (Number) var;

        if (type == Integer.class) {
            return new Integer(newNum.intValue());
        } else if (type == Long.class) {
            return new Long(newNum.longValue());
        } else if (type == Float.class) {
            return new Float(newNum.floatValue());
        } else if (type == Double.class) {
            return new Double(newNum.doubleValue());
        } else if (type == String.class) {
            return new String(newNum.toString());
        }
    } else { //direct cast

        if (type == Integer.class) {
            return new Integer(((Integer) var).intValue());
        } else if (type == Long.class) {
            return new Long(((Long) var).longValue());
        } else if (type == Float.class) {
            return new Float(((Float) var).floatValue());
        } else if (type == Double.class) {
            return new Double(((Double) var).doubleValue());
        } else if (type == String.class) {
            return new String(var.toString());
        }
    }

    return null;
}