Example usage for java.lang Float valueOf

List of usage examples for java.lang Float valueOf

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static Float valueOf(float f) 

Source Link

Document

Returns a Float instance representing the specified float value.

Usage

From source file:com.tibbo.linkserver.plugin.device.file.item.NumericItem.java

/**
 *
 * @param data/*from  ww w  .  j  ava  2s  .  co  m*/
 * @param offset
 * @return
 */
@Override
public Object bytesToValueRealOffset(byte data[], int offset) {
    offset *= 2;
    if (dataType == 2) {
        return Integer.valueOf((data[offset] & 0xff) << 8 | data[offset + 1] & 0xff);
    }
    if (dataType == 3) {
        return Short.valueOf((short) ((data[offset] & 0xff) << 8 | data[offset + 1] & 0xff));
    }
    if (dataType == 16) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 2; i++) {
            sb.append(bcdNibbleToInt(data[offset + i], true));
            sb.append(bcdNibbleToInt(data[offset + i], false));
        }

        return Short.valueOf(Short.parseShort(sb.toString()));
    }
    if (dataType == 4) {
        return Long.valueOf((long) (data[offset] & 0xff) << 24 | (long) (data[offset + 1] & 0xff) << 16
                | (long) (data[offset + 2] & 0xff) << 8 | (long) (data[offset + 3] & 0xff));
    }
    if (dataType == 5) {
        return Integer.valueOf((data[offset] & 0xff) << 24 | (data[offset + 1] & 0xff) << 16
                | (data[offset + 2] & 0xff) << 8 | data[offset + 3] & 0xff);
    }
    if (dataType == 6) {
        return Long.valueOf((long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff));
    }
    if (dataType == 7) {
        return Integer.valueOf((data[offset + 2] & 0xff) << 24 | (data[offset + 3] & 0xff) << 16
                | (data[offset] & 0xff) << 8 | data[offset + 1] & 0xff);
    }
    if (dataType == 8) {
        return Float.valueOf(Float.intBitsToFloat((data[offset] & 0xff) << 24 | (data[offset + 1] & 0xff) << 16
                | (data[offset + 2] & 0xff) << 8 | data[offset + 3] & 0xff));
    }
    if (dataType == 9) {
        return Float.valueOf(Float.intBitsToFloat((data[offset + 2] & 0xff) << 24
                | (data[offset + 3] & 0xff) << 16 | (data[offset] & 0xff) << 8 | data[offset + 1] & 0xff));
    }
    if (dataType == 17) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            sb.append(bcdNibbleToInt(data[offset + i], true));
            sb.append(bcdNibbleToInt(data[offset + i], false));
        }

        return Integer.valueOf(Integer.parseInt(sb.toString()));
    }
    if (dataType == 10) {
        byte b9[] = new byte[9];
        System.arraycopy(data, offset, b9, 1, 8);
        return new BigInteger(b9);
    }
    if (dataType == 11) {
        return Long.valueOf((long) (data[offset] & 0xff) << 56 | (long) (data[offset + 1] & 0xff) << 48
                | (long) (data[offset + 2] & 0xff) << 40 | (long) (data[offset + 3] & 0xff) << 32
                | (long) (data[offset + 4] & 0xff) << 24 | (long) (data[offset + 5] & 0xff) << 16
                | (long) (data[offset + 6] & 0xff) << 8 | (long) (data[offset + 7] & 0xff));
    }
    if (dataType == 12) {
        byte b9[] = new byte[9];
        b9[1] = data[offset + 6];
        b9[2] = data[offset + 7];
        b9[3] = data[offset + 4];
        b9[4] = data[offset + 5];
        b9[5] = data[offset + 2];
        b9[6] = data[offset + 3];
        b9[7] = data[offset];
        b9[8] = data[offset + 1];
        return new BigInteger(b9);
    }
    if (dataType == 13) {
        return Long.valueOf((long) (data[offset + 6] & 0xff) << 56 | (long) (data[offset + 7] & 0xff) << 48
                | (long) (data[offset + 4] & 0xff) << 40 | (long) (data[offset + 5] & 0xff) << 32
                | (long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff));
    }
    if (dataType == 14) {
        return Double.valueOf(Double
                .longBitsToDouble((long) (data[offset] & 0xff) << 56 | (long) (data[offset + 1] & 0xff) << 48
                        | (long) (data[offset + 2] & 0xff) << 40 | (long) (data[offset + 3] & 0xff) << 32
                        | (long) (data[offset + 4] & 0xff) << 24 | (long) (data[offset + 5] & 0xff) << 16
                        | (long) (data[offset + 6] & 0xff) << 8 | (long) (data[offset + 7] & 0xff)));
    }
    if (dataType == 15) {
        return Double.valueOf(Double.longBitsToDouble(
                (long) (data[offset + 6] & 0xff) << 56 | (long) (data[offset + 7] & 0xff) << 48
                        | (long) (data[offset + 4] & 0xff) << 40 | (long) (data[offset + 5] & 0xff) << 32
                        | (long) (data[offset + 2] & 0xff) << 24 | (long) (data[offset + 3] & 0xff) << 16
                        | (long) (data[offset] & 0xff) << 8 | (long) (data[offset + 1] & 0xff)));
    } else {
        throw new RuntimeException(
                (new StringBuilder()).append("Unsupported data type: ").append(dataType).toString());
    }
}

From source file:cn.austin.crawle.Parser.java

public List<Book> parsing(String html) throws XpathSyntaxErrorException {

    //        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    //        domFactory.setNamespaceAware(true);
    //        DocumentBuilder builder = domFactory.newDocumentBuilder();
    ////from  w  w  w . j a  va2  s.  c  o m
    //        InputStream ins = new ByteArrayInputStream(html.getBytes("UTF-8"));
    //        Document doc = builder.parse(ins);
    //
    //        XPath xPath = XPathFactory.newInstance().newXPath();
    //        NodeList nodeList = (NodeList) xPath.compile("//div[@id='content']/div/div[@class = 'article']/ul[class='sub-list']").evaluate(doc, XPathConstants.NODESET);
    //        List<Book> books = new ArrayList<>();
    //        String name = null;
    //        String info = null;
    //        float score = 0f;
    //        int commentNum = 0;
    //        String addition = null;
    //        for (int i = 0; i < nodeList.getLength(); i++) {
    //            Node node = nodeList.item(i);
    //            name = (String) xPath.compile("div[@class='info']/h2/a/text()").evaluate(node, XPathConstants.STRING);
    //            info = (String) xPath.compile("div[@class='info']/div[class='pub']/text()").evaluate(node, XPathConstants.STRING);
    //            String scoreString = (String) xPath.compile("div[@class='info']/div[class='star clearfix']/span[class ='rating_nums']/text()").evaluate(node, XPathConstants.STRING);
    //            score = Float.valueOf(scoreString);
    //            String commentsNumString = (String) xPath.compile("div[@class='info']/div[class='star clearfix']/span[class ='pl']/text()").evaluate(node, XPathConstants.STRING);
    //            commentNum = Integer.valueOf(commentsNumString);//Regex
    //            addition = (String) xPath.compile("div[@class='info']/div[class='ft']/span[class ='buy-info']/a/text()").evaluate(node, XPathConstants.STRING);
    //            books.add(new Book(name, info, score, commentNum, addition));
    //        }
    //        ins.close();
    //        return books;
    JXDocument jxDocument = new JXDocument(html);
    //        String xpath = "//div[@id='content']/div/div/ul[@class='subject-list']/li[@[class='subject-item']";
    String xpath = "//div[@id='content']/div/div/ul/li";
    List<JXNode> jxNodeList = jxDocument.selN(xpath);

    List<Book> books = new ArrayList<>();
    String name;
    String info;
    float score;
    int commentNum;
    String addition;

    for (JXNode node : jxNodeList) {
        name = StringUtils.join(node.sel("div[@class='info']/h2/a/text()"), "");
        info = StringUtils.join(node.sel("div[@class='info']/div[@class='pub']/text()"), "");
        String scoreString = (String) StringUtils.join(
                node.sel("div[@class='info']/div[@class='star clearfix']/span[@class ='rating_nums']/text()"),
                "");
        score = scoreString.trim().equals("") ? 0f : Float.valueOf(scoreString);
        String commentsNumString = (String) StringUtils
                .join(node.sel("div[@class='info']/div[@class='star clearfix']/span[@class ='pl']/text()"), "");
        Matcher matcher = Pattern.compile("\\d+").matcher(commentsNumString);
        String temp = null;
        while (matcher.find()) {
            temp = matcher.group();
        }
        commentNum = temp == null ? 0 : Integer.valueOf(temp);

        addition = StringUtils
                .join(node.sel("div[@class='info']/div[@class='ft']/span[class ='buy-info']/a/text()"), "");
        books.add(new Book(name, info, score, commentNum, addition));
    }
    return books;
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 *
 * @param text        the text to convert
 * @param targetClass the target class to parse into
 * @return the parsed number/* w  ww  .ja v  a 2 s  .  c  o m*/
 * @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#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class targetClass) {
    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:com.spstudio.modules.sales.service.impl.SaleServiceImpl.java

@Override
// //from ww  w  .  j a v a2 s . co m
public Float getGlobalDepositRate() {
    SystemConfig config = configService.findModuleSingleConfig(Configuration.SALE_MODULE_NAME,
            Configuration.CONFIG_GLOBAL_DEPOSIT_BONUSRATE);
    if (config == null) {
        return 0.0f;
    }
    try {
        float bonusRate = Float.valueOf(config.getConfigValue());
        return bonusRate;
    } catch (NumberFormatException ex) {
        ex.printStackTrace();
        return 0.0f;
    }
}

From source file:com.example.android.popularmovies.app.FetchMoviesTask.java

protected ArrayList<HashMap<String, String>> appendMoviesDataFromJsonToAdapter(String jsonString)
        throws JSONException {

    // These are the names of the JSON objects that need to be extracted.
    final String OPM_LIST = "results";
    final String OPM_ID = "id";
    final String OPM_TITLE = "original_title";
    final String OPM_DESCRIPTION = "overview";
    final String OPM_RATE = "vote_average";
    final String OPM_DATE = "release_date";
    final String OPM_LANG = "original_language";
    final String OPM_ADULT = "adult";
    final String OPM_IMG = "poster_path";
    final String OPM_PAGE = "page";

    // define list of all movies
    ArrayList<HashMap<String, String>> moviesData = new ArrayList<HashMap<String, String>>();

    JSONObject moviesJson = new JSONObject(jsonString);

    int page = Integer.valueOf(moviesJson.getString(OPM_PAGE));

    // verify response page number and update the adapter page number
    if (page > 0) {
        moviesAdapter.setCurrentPage(page);
    } else//w ww .j  a v a2  s. co  m
        return moviesData;

    JSONArray moviesArray = moviesJson.getJSONArray(OPM_LIST);

    for (int i = 0; i < moviesArray.length(); i++) {

        // Get the JSON object representing the one movie row
        JSONObject movieRow = moviesArray.getJSONObject(i);
        HashMap<String, String> mItem = new HashMap<String, String>();

        mItem.put(MoviesAdapter.HASH_MAP_KEY_ID, movieRow.getString(OPM_ID));
        mItem.put(MoviesAdapter.HASH_MAP_KEY_TITLE, movieRow.getString(OPM_TITLE));
        mItem.put(MoviesAdapter.HASH_MAP_KEY_DESCRIPTION, movieRow.getString(OPM_DESCRIPTION));
        mItem.put(MoviesAdapter.HASH_MAP_KEY_DATE, beautifyDate(movieRow.getString(OPM_DATE)));
        mItem.put(MoviesAdapter.HASH_MAP_KEY_RATE,
                convertRateFrom10To5Stars(Float.valueOf(movieRow.getString(OPM_RATE))));
        String movieAges = mContext.getString(R.string.movie_adult_all);
        if (movieRow.getString(OPM_ADULT).equalsIgnoreCase("true")) {
            movieAges = mContext.getString(R.string.movie_adult_plus_18);
        }
        mItem.put(MoviesAdapter.HASH_MAP_KEY_ADULT, movieAges);
        mItem.put(MoviesAdapter.HASH_MAP_KEY_LANG, movieRow.getString(OPM_LANG));
        mItem.put(MoviesAdapter.HASH_MAP_KEY_IMAGE, movieRow.getString(OPM_IMG));

        moviesData.add(mItem);
        //moviesAdapter.appendMovie(mItem);

    }

    return moviesData;
}

From source file:com.hortonworks.pso.data.generator.fields.NumberField.java

protected Number newValue() {
    switch (type) {
    case INT:/*from w w w. j  a va 2 s.c om*/
        return (Integer) min + random.nextInt((Integer) getDiff());
    case LONG:
        double multiplierD = random.nextDouble();
        return (Long) min + Math.round((Long) getDiff() * multiplierD);
    case FLOAT:
        float multiplierF = random.nextFloat();
        Float valF = (Float) min + ((Float) getDiff() * multiplierF);
        return Float.valueOf(decimalFormat.format(valF));
    case DOUBLE:
        double multiplierD2 = random.nextDouble();
        Double valD = (Double) min + ((Double) getDiff() * multiplierD2);
        return Double.valueOf(decimalFormat.format(valD));
    default:
        return (Integer) min + random.nextInt((Integer) getDiff());
    }
}

From source file:com.odiago.flumebase.io.CharBufferUtils.java

/**
 * Parses a CharSequence into a floating-point value.
 *//* w ww  .  jav a 2 s . c o m*/
public static float parseFloat(CharBuffer chars) throws ColumnParseException {
    try {
        return Float.valueOf(new String(chars.array()));
    } catch (NumberFormatException nfe) {
        throw new ColumnParseException(nfe);
    }
}

From source file:com.sm.query.utils.QueryUtils.java

public static Object getParamArg(Class<?> cl) {
    if (!cl.isPrimitive())
        return null;
    else if (boolean.class.equals(cl))
        return Boolean.FALSE;
    else if (byte.class.equals(cl))
        return new Byte((byte) 0);
    else if (short.class.equals(cl))
        return new Short((short) 0);
    else if (char.class.equals(cl))
        return new Character((char) 0);
    else if (int.class.equals(cl))
        return Integer.valueOf(0);
    else if (long.class.equals(cl))
        return Long.valueOf(0);
    else if (float.class.equals(cl))
        return Float.valueOf(0);
    else if (double.class.equals(cl))
        return Double.valueOf(0);
    else//w w w .  j a va2  s.co m
        throw new UnsupportedOperationException();
}

From source file:org.glenans.extractor.model.Stage.java

private float extractDuration(String duration) {
    if (!duration.contains(" ")) {
        return Float.valueOf(duration);
    }/*from w  w  w  .  j  av a  2  s .  c o  m*/
    return Float.valueOf(duration.substring(0, duration.indexOf(" ")).trim());
}

From source file:de.odysseus.calyxo.base.util.ParseUtils.java

/**
     * Parse value of specified type. The string value has to be in
     * standard notation for the specified type.
     *//*  w  w  w  . j ava2 s  .  c  o  m*/
    public static Object parse(Class type, String value) throws Exception {
        if (value == null) {
            return nullValue(type);
        } else if (value.length() == 0) {
            return type == String.class ? value : nullValue(type);
        }

        type = objectType(type);

        if (type == BigDecimal.class) {
            return new BigDecimal(value);
        } else if (type == BigInteger.class) {
            return new BigInteger(value);
        } else if (type == Boolean.class) {
            return parseBoolean(value);
        } else if (type == Byte.class) {
            return Byte.valueOf(value);
        } else if (type == Character.class) {
            return parseCharacter(value);
        } else if (type == Date.class) {
            return parseDate(value);
        } else if (type == Double.class) {
            return Double.valueOf(value);
        } else if (type == Float.class) {
            return Float.valueOf(value);
        } else if (type == Integer.class) {
            return Integer.valueOf(value);
        } else if (type == Long.class) {
            return Long.valueOf(value);
        } else if (type == Short.class) {
            return Short.valueOf(value);
        } else if (type == String.class) {
            return value;
        }
        throw new ParseException("Cannot parse type " + type, 0);
    }