Example usage for java.lang Float Float

List of usage examples for java.lang Float Float

Introduction

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

Prototype

@Deprecated(since = "9")
public Float(String s) throws NumberFormatException 

Source Link

Document

Constructs a newly allocated Float object that represents the floating-point value of type float represented by the string.

Usage

From source file:javadz.beanutils.converters.NumberConverter.java

/**
 * Convert any Number object to the specified type for this
 * <i>Converter</i>.// w ww  .j  av  a2  s  .c om
 * <p>
 * This method handles conversion to the following types:
 * <ul>
 *     <li><code>java.lang.Byte</code></li>
 *     <li><code>java.lang.Short</code></li>
 *     <li><code>java.lang.Integer</code></li>
 *     <li><code>java.lang.Long</code></li>
 *     <li><code>java.lang.Float</code></li>
 *     <li><code>java.lang.Double</code></li>
 *     <li><code>java.math.BigDecimal</code></li>
 *     <li><code>java.math.BigInteger</code></li>
 * </ul>
 * @param sourceType The type being converted from
 * @param targetType The Number type to convert to
 * @param value The Number to convert.
 *
 * @return The converted value.
 */
private Number toNumber(Class sourceType, Class targetType, Number value) {

    // Correct Number type already
    if (targetType.equals(value.getClass())) {
        return value;
    }

    // Byte
    if (targetType.equals(Byte.class)) {
        long longValue = value.longValue();
        if (longValue > Byte.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Byte.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Byte(value.byteValue());
    }

    // Short
    if (targetType.equals(Short.class)) {
        long longValue = value.longValue();
        if (longValue > Short.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Short.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Short(value.shortValue());
    }

    // Integer
    if (targetType.equals(Integer.class)) {
        long longValue = value.longValue();
        if (longValue > Integer.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Integer.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Integer(value.intValue());
    }

    // Long
    if (targetType.equals(Long.class)) {
        return new Long(value.longValue());
    }

    // Float
    if (targetType.equals(Float.class)) {
        if (value.doubleValue() > Float.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        return new Float(value.floatValue());
    }

    // Double
    if (targetType.equals(Double.class)) {
        return new Double(value.doubleValue());
    }

    // BigDecimal
    if (targetType.equals(BigDecimal.class)) {
        if (value instanceof Float || value instanceof Double) {
            return new BigDecimal(value.toString());
        } else if (value instanceof BigInteger) {
            return new BigDecimal((BigInteger) value);
        } else {
            return BigDecimal.valueOf(value.longValue());
        }
    }

    // BigInteger
    if (targetType.equals(BigInteger.class)) {
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).toBigInteger();
        } else {
            return BigInteger.valueOf(value.longValue());
        }
    }

    String msg = toString(getClass()) + " cannot handle conversion to '" + toString(targetType) + "'";
    if (log().isWarnEnabled()) {
        log().warn("    " + msg);
    }
    throw new ConversionException(msg);

}

From source file:com.redhat.lightblue.metadata.parser.JSONMetadataParserTest.java

License:asdf

@Test
public void putValueFloat() {
    ObjectNode parent = new ObjectNode(factory);
    String name = "foo";
    Float value = new Float("123.222");

    parser.putValue(parent, name, value);

    JsonNode x = parent.get(name);/*from  w  w  w . j  av a  2  s. com*/

    Assert.assertNotNull(x);
    Assert.assertEquals(value.floatValue(), x.floatValue(), 0.001);
}

From source file:com.gisgraphy.importer.GeonamesZipCodeImporterTest.java

@Test
public void findFeatureBasicWithSeveralResult() {
    String lat = "3.5";
    String lng = "44";
    String accuracy = "5";
    String placeName = "place name";
    String countryCode = "FR";
    String adm1Name = "adm1name";
    String adm1Code = "adm1code";
    String adm2Name = "adm2name";
    String adm2Code = "adm2code";
    String adm3Name = "adm3name";
    String adm3COde = "adm3code";
    FulltextQuery fulltextQuery = new FulltextQuery(placeName + " " + adm1Name);
    fulltextQuery.limitToCountryCode(countryCode);
    fulltextQuery.withPlaceTypes(com.gisgraphy.fulltext.Constants.CITY_AND_CITYSUBDIVISION_PLACETYPE);
    final long featureId = 456L;

    GeonamesZipCodeSimpleImporter importer = new GeonamesZipCodeSimpleImporter() {

        @Override/*w  w  w . j  a va 2 s  .co m*/
        protected FulltextResultsDto doAFulltextSearch(String query, String countryCode) {
            return dtoWithTwoResults;
        }

        @Override
        protected Long findNearest(Point zipPoint, int maxDistance, FulltextResultsDto results) {
            return featureId;
        }
    };
    String[] fields = { countryCode, "post", placeName, adm1Name, adm1Code, adm2Name, adm2Code, adm3Name,
            adm3COde, lat, lng, accuracy };
    Point point = GeolocHelper.createPoint(new Float(lng), new Float(lat));
    int maxDistance = importer.getAccurateDistance(new Integer(accuracy));
    Long actualFeatureId = importer.findFeature(fields, point, maxDistance);
    Assert.assertEquals(new Long(featureId), actualFeatureId);

}

From source file:gemlite.core.internal.measurement.index.BigComparator.java

private Object transformParameter(String key, String value) {
    Object result = null;/*from  w w w .j  av a  2s .c o m*/
    String type = getKeyFields().get(key);
    if (type.equalsIgnoreCase("String"))
        result = value;
    else if (type.equalsIgnoreCase("int"))
        result = Integer.parseInt(value);
    else if (type.equalsIgnoreCase("Integer"))
        result = new Integer(value);
    else if (type.equalsIgnoreCase("long"))
        result = Long.parseLong(value);
    else if (type.equalsIgnoreCase("Long"))
        result = new Long(value);
    else if (type.equalsIgnoreCase("double"))
        result = Double.parseDouble(value);
    else if (type.equalsIgnoreCase("Double"))
        result = new Double(value);
    else if (type.equalsIgnoreCase("float"))
        result = Float.parseFloat(value);
    else if (type.equalsIgnoreCase("Float"))
        result = new Float(value);
    else if (type.equalsIgnoreCase("boolean"))
        result = Boolean.parseBoolean(value);
    else if (type.equalsIgnoreCase("Boolean"))
        result = new Boolean(value);
    else if (type.equalsIgnoreCase("Date"))
        result = DateUtil.parse(value);
    else
        result = value;

    return result;

}

From source file:com.itelis.worker.dev.template.service.JRXmlDataSource.java

protected Object convertNumber(Number number, Class valueClass) throws JRException {
    Number value = null;/*from   w w  w. ja  v a2  s . c om*/
    if (valueClass.equals(Byte.class)) {
        value = new Byte(number.byteValue());
    } else if (valueClass.equals(Short.class)) {
        value = new Short(number.shortValue());
    } else if (valueClass.equals(Integer.class)) {
        value = new Integer(number.intValue());
    } else if (valueClass.equals(Long.class)) {
        value = new Long(number.longValue());
    } else if (valueClass.equals(Float.class)) {
        value = new Float(number.floatValue());
    } else if (valueClass.equals(Double.class)) {
        value = new Double(number.doubleValue());
    } else if (valueClass.equals(BigInteger.class)) {
        value = BigInteger.valueOf(number.longValue());
    } else if (valueClass.equals(BigDecimal.class)) {
        value = new BigDecimal(Double.toString(number.doubleValue()));
    } else {
        throw new JRException("Unknown number class " + valueClass.getName());
    }
    return value;
}

From source file:com.espertech.esper.regression.expr.TestCaseExpr.java

public void testCaseSyntax2StringsNBranches() {
    // Test of the various coercion user cases.
    String caseExpr = "select case intPrimitive" + " when 1 then Boolean.toString(boolPrimitive) "
            + " when 2 then Boolean.toString(boolBoxed) " + " when 3 then Integer.toString(intPrimitive) "
            + " when 4 then Integer.toString(intBoxed)" + " when 5 then Long.toString(longPrimitive) "
            + " when 6 then Long.toString(longBoxed) " + " when 7 then Character.toString(charPrimitive) "
            + " when 8 then Character.toString(charBoxed) " + " when 9 then Short.toString(shortPrimitive) "
            + " when 10 then Short.toString(shortBoxed) " + " when 11 then Byte.toString(bytePrimitive) "
            + " when 12 then Byte.toString(byteBoxed) " + " when 13 then Float.toString(floatPrimitive) "
            + " when 14 then Float.toString(floatBoxed) " + " when 15 then Double.toString(doublePrimitive) "
            + " when 16 then Double.toString(doubleBoxed) " + " when 17 then theString "
            + " else 'x' end as p1 " + " from " + SupportBean.class.getName() + ".win:length(1)";

    EPStatement selectTestCase = epService.getEPAdministrator().createEPL(caseExpr);
    selectTestCase.addListener(testListener);
    assertEquals(String.class, selectTestCase.getEventType().getPropertyType("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 1, new Integer(0), 0L, new Long(0L), '0', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    EventBean theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("true", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 2, new Integer(0), 0L, new Long(0L), '0', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("false", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 3, new Integer(0), 0L, new Long(0L), '0', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("3", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 4, new Integer(4), 0L, new Long(0L), '0', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("4", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 5, new Integer(0), 5L, new Long(0L), '0', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("5", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 6, new Integer(0), 0L, new Long(6L), '0', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("6", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 7, new Integer(0), 0L, new Long(0L), 'A', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("A", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 8, new Integer(0), 0L, new Long(0L), 'A', new Character('a'),
            (short) 0, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("a", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 9, new Integer(0), 0L, new Long(0L), 'A', new Character('a'),
            (short) 9, new Short((short) 0), (byte) 0, new Byte((byte) 0), 0.0f, new Float((float) 0), 0.0,
            new Double(0.0), null, SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("9", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 10, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("10", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 11, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("11", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 12, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("12", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 13, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("13.0", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 14, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("14.0", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 15, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("15.0", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 16, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("16.0", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), 17, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("testCoercion", theEvent.get("p1"));

    sendSupportBeanEvent(true, new Boolean(false), -1, new Integer(0), 0L, new Long(0L), 'A',
            new Character('a'), (short) 9, new Short((short) 10), (byte) 11, new Byte((byte) 12), 13.0f,
            new Float((float) 14), 15.0, new Double(16.0), "testCoercion", SupportEnum.ENUM_VALUE_1);
    theEvent = testListener.getAndResetLastNewData()[0];
    assertEquals("x", theEvent.get("p1"));
}

From source file:es.caib.zkib.jxpath.util.BasicTypeConverter.java

/**
 * Allocate a number of a given type and value.
 * @param type destination class//from   w ww.  ja v a  2s.c  o  m
 * @param value double
 * @return Number
 */
protected Number allocateNumber(Class type, double value) {
    type = TypeUtils.wrapPrimitive(type);
    if (type == Byte.class) {
        return new Byte((byte) value);
    }
    if (type == Short.class) {
        return new Short((short) value);
    }
    if (type == Integer.class) {
        return new Integer((int) value);
    }
    if (type == Long.class) {
        return new Long((long) value);
    }
    if (type == Float.class) {
        return new Float((float) value);
    }
    if (type == Double.class) {
        return new Double(value);
    }
    if (type == BigInteger.class) {
        return BigInteger.valueOf((long) value);
    }
    if (type == BigDecimal.class) {
        return new BigDecimal(value);
    }
    String classname = type.getName();
    Class initialValueType = null;
    if ("java.util.concurrent.atomic.AtomicInteger".equals(classname)) {
        initialValueType = int.class;
    }
    if ("java.util.concurrent.atomic.AtomicLong".equals(classname)) {
        initialValueType = long.class;
    }
    if (initialValueType != null) {
        try {
            return (Number) type.getConstructor(new Class[] { initialValueType })
                    .newInstance(new Object[] { allocateNumber(initialValueType, value) });
        } catch (Exception e) {
            throw new JXPathTypeConversionException(classname, e);
        }
    }
    return null;
}

From source file:com.xpn.xwiki.objects.BaseCollection.java

public void setFloatValue(String name, float value) {
    NumberProperty property = new FloatProperty();
    property.setName(name);//from   ww w  .j a  va 2 s  .com
    property.setValue(new Float(value));
    safeput(name, property);
}

From source file:de.topicmapslab.couchtm.core.OccurrenceImpl.java

@Override
public void setValue(float value) {
    if (!loaded)/*w  w  w .j av a2s .c o m*/
        load();
    if (mergedIn != null) {
        mergedIn.setValue(value);
        return;
    }
    datatype = new LocatorImpl(IConstant.XSD_FLOAT);
    this.value = (new Float(value)).toString();
    try {
        _fireEvent(Event.VALUE_CHANGED, null, value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}