Example usage for java.lang Float parseFloat

List of usage examples for java.lang Float parseFloat

Introduction

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

Prototype

public static float parseFloat(String s) throws NumberFormatException 

Source Link

Document

Returns a new float initialized to the value represented by the specified String , as performed by the valueOf method of class Float .

Usage

From source file:com.evolveum.midpoint.prism.util.JavaTypeConverter.java

public static <T> T convert(Class<T> expectedType, Object rawValue) {
    if (rawValue == null || expectedType.isInstance(rawValue)) {
        return (T) rawValue;
    }//  w ww. j  a v  a  2 s  . c o  m
    if (rawValue instanceof PrismPropertyValue<?>) {
        rawValue = ((PrismPropertyValue<?>) rawValue).getValue();
    }
    // This really needs to be checked twice
    if (rawValue == null || expectedType.isInstance(rawValue)) {
        return (T) rawValue;
    }

    // Primitive types

    // boolean
    if (expectedType == boolean.class && rawValue instanceof Boolean) {
        return (T) ((Boolean) rawValue);
    }
    if (expectedType == Boolean.class && rawValue instanceof String) {
        return (T) (Boolean) Boolean.parseBoolean(((String) rawValue));
    }
    if (expectedType == Boolean.class && rawValue instanceof PolyString) {
        return (T) (Boolean) Boolean.parseBoolean(((PolyString) rawValue).toString());
    }
    if (expectedType == boolean.class && rawValue instanceof String) {
        return (T) (Boolean) Boolean.parseBoolean(((String) rawValue));
    }
    if (expectedType == boolean.class && rawValue instanceof PolyString) {
        return (T) (Boolean) Boolean.parseBoolean(((PolyString) rawValue).toString());
    }
    if (expectedType == String.class && rawValue instanceof Boolean) {
        return (T) rawValue.toString();
    }

    // int
    if (expectedType == int.class && rawValue instanceof Integer) {
        return (T) ((Integer) rawValue);
    }
    if (expectedType == Integer.class && rawValue instanceof String) {
        return (T) (Integer) Integer.parseInt(((String) rawValue));
    }
    if (expectedType == int.class && rawValue instanceof String) {
        return (T) (Integer) Integer.parseInt(((String) rawValue));
    }
    if (expectedType == String.class && rawValue instanceof Integer) {
        return (T) rawValue.toString();
    }
    if (expectedType == int.class && rawValue instanceof Long) {
        return (T) (Integer) ((Long) rawValue).intValue();
    }

    if (expectedType == long.class && rawValue instanceof Long) {
        return (T) ((Long) rawValue);
    }
    if (expectedType == Long.class && rawValue instanceof String) {
        return (T) (Long) Long.parseLong(((String) rawValue));
    }
    if (expectedType == long.class && rawValue instanceof String) {
        return (T) (Long) Long.parseLong(((String) rawValue));
    }
    if (expectedType == String.class && rawValue instanceof Long) {
        return (T) rawValue.toString();
    }

    if (expectedType == float.class && rawValue instanceof Float) {
        return (T) ((Float) rawValue);
    }
    if (expectedType == Float.class && rawValue instanceof String) {
        return (T) (Float) Float.parseFloat(((String) rawValue));
    }
    if (expectedType == float.class && rawValue instanceof String) {
        return (T) (Float) Float.parseFloat(((String) rawValue));
    }
    if (expectedType == String.class && rawValue instanceof Float) {
        return (T) rawValue.toString();
    }

    if (expectedType == double.class && rawValue instanceof Double) {
        return (T) ((Double) rawValue);
    }
    if (expectedType == Double.class && rawValue instanceof String) {
        return (T) (Double) Double.parseDouble(((String) rawValue));
    }
    if (expectedType == double.class && rawValue instanceof String) {
        return (T) (Double) Double.parseDouble(((String) rawValue));
    }
    if (expectedType == String.class && rawValue instanceof Float) {
        return (T) rawValue.toString();
    }

    if (expectedType == byte.class && rawValue instanceof Byte) {
        return (T) ((Byte) rawValue);
    }
    if (expectedType == Byte.class && rawValue instanceof String) {
        return (T) (Byte) Byte.parseByte(((String) rawValue));
    }
    if (expectedType == byte.class && rawValue instanceof String) {
        return (T) (Byte) Byte.parseByte(((String) rawValue));
    }
    if (expectedType == String.class && rawValue instanceof Byte) {
        return (T) rawValue.toString();
    }

    if (expectedType == BigInteger.class && rawValue instanceof String) {
        return (T) new BigInteger(((String) rawValue));
    }
    if (expectedType == String.class && rawValue instanceof BigInteger) {
        return (T) ((BigInteger) rawValue).toString();
    }

    if (expectedType == BigDecimal.class && rawValue instanceof String) {
        return (T) new BigDecimal(((String) rawValue));
    }
    if (expectedType == String.class && rawValue instanceof BigDecimal) {
        return (T) ((BigDecimal) rawValue).toString();
    }

    if (expectedType == PolyString.class && rawValue instanceof String) {
        return (T) new PolyString((String) rawValue);
    }
    if (expectedType == PolyStringType.class && rawValue instanceof String) {
        PolyStringType polyStringType = new PolyStringType();
        polyStringType.setOrig((String) rawValue);
        return (T) polyStringType;
    }
    if (expectedType == String.class && rawValue instanceof PolyString) {
        return (T) ((PolyString) rawValue).getOrig();
    }
    if (expectedType == String.class && rawValue instanceof PolyStringType) {
        return (T) ((PolyStringType) rawValue).getOrig();
    }
    if (expectedType == PolyString.class && rawValue instanceof PolyStringType) {
        return (T) ((PolyStringType) rawValue).toPolyString();
    }
    if (expectedType == PolyString.class && rawValue instanceof Integer) {
        return (T) new PolyString(((Integer) rawValue).toString());
    }
    if (expectedType == PolyStringType.class && rawValue instanceof PolyString) {
        PolyStringType polyStringType = new PolyStringType((PolyString) rawValue);
        return (T) polyStringType;
    }
    if (expectedType == PolyStringType.class && rawValue instanceof Integer) {
        PolyStringType polyStringType = new PolyStringType(((Integer) rawValue).toString());
        return (T) polyStringType;
    }

    // Date and time
    if (expectedType == XMLGregorianCalendar.class && rawValue instanceof Long) {
        XMLGregorianCalendar xmlCalType = XmlTypeConverter.createXMLGregorianCalendar((Long) rawValue);
        return (T) xmlCalType;
    }
    if (expectedType == XMLGregorianCalendar.class && rawValue instanceof String) {
        XMLGregorianCalendar xmlCalType = magicDateTimeParse((String) rawValue);
        return (T) xmlCalType;
    }
    if (expectedType == String.class && rawValue instanceof XMLGregorianCalendar) {
        return (T) ((XMLGregorianCalendar) rawValue).toXMLFormat();
    }
    if (expectedType == Long.class && rawValue instanceof XMLGregorianCalendar) {
        return (T) (Long) XmlTypeConverter.toMillis((XMLGregorianCalendar) rawValue);
    }

    // XML Enums (JAXB)
    if (expectedType.isEnum() && expectedType.getAnnotation(XmlEnum.class) != null
            && rawValue instanceof String) {
        return XmlTypeConverter.toXmlEnum(expectedType, (String) rawValue);
    }
    if (expectedType == String.class && rawValue.getClass().isEnum()
            && rawValue.getClass().getAnnotation(XmlEnum.class) != null) {
        return (T) XmlTypeConverter.fromXmlEnum(rawValue);
    }

    // Java Enums
    if (expectedType.isEnum() && rawValue instanceof String) {
        return (T) Enum.valueOf((Class<Enum>) expectedType, (String) rawValue);
    }
    if (expectedType == String.class && rawValue.getClass().isEnum()) {
        return (T) rawValue.toString();
    }

    //QName
    if (expectedType == QName.class && rawValue instanceof QName) {
        return (T) rawValue;
    }
    if (expectedType == QName.class && rawValue instanceof String) {
        return (T) QNameUtil.uriToQName((String) rawValue);
    }

    throw new IllegalArgumentException("Expected " + expectedType + " type, but got " + rawValue.getClass());
}

From source file:com.googlecode.psiprobe.Utils.java

public static float toFloat(String num, float defaultValue) {
    if (num != null) {
        try {// w w  w  .j  av  a  2  s.c  om
            return Float.parseFloat(num);
        } catch (NumberFormatException e) {
            //ignore
        }
    }
    return defaultValue;
}

From source file:com.brewtab.ircbot.applets.WundergroundApplet.java

private String formatTemp(String s) {
    int value = (int) Float.parseFloat(s);
    return Integer.toString(value);
}

From source file:com.eyeq.pivot4j.ui.property.PropertySupport.java

/**
 * @param name/*  w  ww .j ava2 s. c om*/
 * @param defaultValue
 * @param context
 * @return
 */
public Float getFloat(String name, Float defaultValue, RenderContext context) {
    Float floatValue = null;

    String value = getString(name, null, context);

    if (value != null) {
        floatValue = Float.parseFloat(value);
    }

    if (floatValue == null) {
        floatValue = defaultValue;
    }

    return floatValue;
}

From source file:com.thruzero.common.core.utils.StringUtilsExt.java

public static float stringToFloat(final String value, final float defaultValue) {
    return value == null ? defaultValue : Float.parseFloat(value);
}

From source file:com.ms.app.web.commons.tools.CurrencyFormattor.java

public static float toFloat(String number) {
    try {/*from   w  w  w. ja va  2  s.c  o m*/
        return Float.parseFloat(number);
    } catch (Exception e) {
        return 0;
    }
}

From source file:me.willowcheng.makerthings.model.OpenHABItem.java

public float[] getStateAsHSV() {
    if (state == null) {
        float[] result = { 0, 0, 0 };
        return result;
    }/*from w w w.j  a  v  a2s.  c  o  m*/
    String[] stateSplit = state.split(",");
    if (stateSplit.length == 3) { // We need exactly 3 numbers to operate this
        float[] result = { Float.parseFloat(stateSplit[0]), Float.parseFloat(stateSplit[1]) / 100,
                Float.parseFloat(stateSplit[2]) / 100 };
        return result;
    } else {
        float[] result = { 0, 0, 0 };
        return result;
    }
}

From source file:com.appassit.common.Utils.java

/**
 * <p>/*from w  w w  . ja va  2s.c om*/
 * Parse float value from string
 * </p>
 * 
 * @param value
 *            string
 * @return float value
 */
public static float getFloat(String value) {
    if (value == null)
        return 0f;

    try {
        return Float.parseFloat(value.trim());
    } catch (NumberFormatException e) {
        return 0f;
    }
}

From source file:account.management.controller.CNFReportController.java

@FXML
private void onShowReportButtonClick(ActionEvent event) {
    String start_date, end_date;/* w  ww .  ja va2  s  . c  o  m*/
    try {
        String lc_id = this.select_cnf.getSelectionModel().getSelectedItem().getId();
        start_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString()));
        end_date = new SimpleDateFormat("yyyy-MM-dd")
                .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString()));
        HttpResponse<JsonNode> res = Unirest.get(MetaData.baseUrl + "report/balancesheet")
                .queryString("date", "\"" + start_date + "\"").queryString("plc", lc_id).asJson();

        HttpResponse<JsonNode> res2 = Unirest.get(MetaData.baseUrl + "report/balancesheet")
                .queryString("date", "\"" + end_date + "\"").queryString("plc", lc_id).asJson();

        JSONArray bs = res.getBody().getArray();
        JSONArray bs2 = res2.getBody().getArray();
        System.out.println(bs);
        System.out.println(bs2);
        HashMap params = new HashMap();
        Vector v = new Vector();
        float total_non_current_asset = 0, total_current_asset = 0, total_revenue = 0, total_expense = 0;
        for (int i = 0; i < bs.length(); i++) {
            JSONObject obj = bs.getJSONObject(i);
            JSONObject obj2 = bs2.getJSONObject(i);
            String id = obj.get("id").toString();
            String balance = String.valueOf(Float.parseFloat(obj2.get("totalBalance").toString())
                    - Float.parseFloat(obj.get("totalBalance").toString()));
            v.add(new BS(id, balance));
            params.put(id, balance);

            // total non current asset
            if (Integer.parseInt(id) >= 9 && Integer.parseInt(id) <= 12) {
                total_non_current_asset += Float.parseFloat(balance);
            }
            // total current asset
            if (Integer.parseInt(id) >= 13 && Integer.parseInt(id) <= 27) {
                total_current_asset += Float.parseFloat(balance);
            }
            // total revenue
            if (Integer.parseInt(id) >= 38 && Integer.parseInt(id) <= 40) {
                total_revenue += Float.parseFloat(balance);
            }
            // total expense
            if ((Integer.parseInt(id) >= 42 && Integer.parseInt(id) <= 44)
                    || (Integer.parseInt(id) >= 46 && Integer.parseInt(id) <= 48)
                    || (Integer.parseInt(id) >= 50 && Integer.parseInt(id) <= 57)) {
                total_expense += Float.parseFloat(balance);
            }
        }
        params.put("date",
                new SimpleDateFormat("dd-MM-yyyy")
                        .format(new SimpleDateFormat("yyyy-MM-dd").parse(this.start.getValue().toString()))
                        + "To " + new SimpleDateFormat("dd-MM-yyyy").format(
                                new SimpleDateFormat("yyyy-MM-dd").parse(this.end.getValue().toString())));
        params.put("total_non_current_asset", String.valueOf(total_non_current_asset));
        params.put("total_current_asset", String.valueOf(total_current_asset));
        params.put("total_revenue", String.valueOf(total_revenue));
        params.put("total_expense", String.valueOf(total_expense));
        params.put("net_profit", String.valueOf(total_revenue - total_expense));
        CNF c = this.select_cnf.getSelectionModel().getSelectedItem();
        params.put("party_name", "Party Name: " + c.getParty_name());
        params.put("party_address", "Party Address: " + c.getParty_address());

        Report r = new Report();
        r.getReport("src\\report\\CNF.jrxml", new JRBeanCollectionDataSource(v), params, "CNF Report");
    } catch (ParseException ex) {
        Logger.getLogger(LCReportController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnirestException ex) {
        Logger.getLogger(LCReportController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:iddb.core.util.Functions.java

public static Long time2minutes(String time) {
    if (time == null || "".trim().equals(time))
        return 0L;

    Float value;//from www  . jav  a 2s.  c om

    char last = time.charAt(time.length() - 1);
    try {
        switch (last) {
        case 'h':
            value = Float.parseFloat(time.substring(0, time.length() - 1)) * 60;
            break;
        case 'm':
            value = Float.parseFloat(time.substring(0, time.length() - 1));
            break;
        case 's':
            value = Float.parseFloat(time.substring(0, time.length() - 1)) / 60;
            break;
        case 'd':
            value = Float.parseFloat(time.substring(0, time.length() - 1)) * 60 * 24;
            break;
        case 'w':
            value = Float.parseFloat(time.substring(0, time.length() - 1)) * 60 * 24 * 7;
            break;
        case 'M':
            value = Float.parseFloat(time.substring(0, time.length() - 1)) * 60 * 24 * 31;
            break;
        case 'y':
            value = Float.parseFloat(time.substring(0, time.length() - 1)) * 60 * 24 * 365;
            break;
        default:
            value = Float.parseFloat(time);
            break;
        }
    } catch (NumberFormatException e) {
        value = 0f;
    }
    return value.longValue();
}