Example usage for org.apache.commons.lang3.math NumberUtils toFloat

List of usage examples for org.apache.commons.lang3.math NumberUtils toFloat

Introduction

In this page you can find the example usage for org.apache.commons.lang3.math NumberUtils toFloat.

Prototype

public static float toFloat(final String str, final float defaultValue) 

Source Link

Document

Convert a String to a float, returning a default value if the conversion fails.

If the string str is null, the default value is returned.

 NumberUtils.toFloat(null, 1.1f)   = 1.0f NumberUtils.toFloat("", 1.1f)     = 1.1f NumberUtils.toFloat("1.5", 0.0f)  = 1.5f 

Usage

From source file:com.omertron.tvrageapi.model.Episode.java

public void setRating(String rating) {
    this.rating = NumberUtils.toFloat(rating, 0.0f);
}

From source file:com.moviejukebox.tools.DateTimeTools.java

/**
 * Take a string runtime in various formats and try to output this in minutes
 *
 * @param runtime/*from   w w w . ja  v  a  2s .co m*/
 * @return
 */
public static int processRuntime(final String runtime) {
    if (StringUtils.isBlank(runtime)) {
        // No string to parse
        return -1;
    }

    // See if we can convert this to a number and assume it's correct if we can
    int returnValue = (int) NumberUtils.toFloat(runtime, -1f);

    if (returnValue < 0) {
        // This is for the format xx(hour/hr/min)yy(min), e.g. 1h30, 90mins, 1h30m
        Pattern hrmnPattern = Pattern.compile("(?i)(\\d+)(\\D*)(\\d*)(.*?)");

        Matcher matcher = hrmnPattern.matcher(runtime);
        if (matcher.find()) {
            int first = NumberUtils.toInt(matcher.group(1), -1);
            String divide = matcher.group(2);
            int second = NumberUtils.toInt(matcher.group(3), -1);

            if (first > -1 && second > -1) {
                returnValue = (first > -1 ? first * 60 : 0) + (second > -1 ? second : 0);
            } else if (isNotValidString(divide)) {
                // No divider value, so assume this is a straight minute value
                returnValue = first;
            } else if (second > -1 && isValidString(divide)) {
                // this is xx(text) so we need to work out what the (text) is
                if (divide.toLowerCase().contains("h")) {
                    // Assume it is a form of "hours", so convert to minutes
                    returnValue = first * 60;
                } else {
                    // Assume it's a form of "minutes"
                    returnValue = first;
                }
            }
        }
    }

    return returnValue;
}

From source file:com.netsteadfast.greenstep.bsc.util.AggregationMethod.java

public float average(KpiVO kpi) throws Exception {
    List<BbMeasureData> measureDatas = kpi.getMeasureDatas();
    float score = 0.0f; // init zero
    int size = 0;
    for (BbMeasureData measureData : measureDatas) {
        BscMeasureData data = new BscMeasureData();
        data.setActual(measureData.getActual());
        data.setTarget(measureData.getTarget());
        try {//  www  .  ja  v  a 2  s  .co m
            Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
            if (value == null) {
                continue;
            }
            if (!NumberUtils.isNumber(String.valueOf(value))) {
                continue;
            }
            score += NumberUtils.toFloat(String.valueOf(value), 0.0f);
            size++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (score != 0.0f && size > 0) {
        score = score / size;
    }
    return score;
}

From source file:com.monarchapis.driver.util.MIMEParse.java

/**
 * Find the best match for a given mimeType against a list of media_ranges
 * that have already been parsed by MimeParse.parseMediaRange(). Returns a
 * tuple of the fitness value and the value of the 'q' quality parameter of
 * the best match, or (-1, 0) if no match was found. Just as for
 * quality_parsed(), 'parsed_ranges' must be a list of parsed media ranges.
 * //from  ww w  .j  av a 2  s.  co  m
 * @param mimeType
 *            The mime type
 * @param parsedRanges
 *            The parsed ranges
 */
protected static FitnessAndQuality fitnessAndQualityParsed(String mimeType,
        Collection<ParseResults> parsedRanges) {
    int bestFitness = -1;
    float bestFitQ = 0;
    ParseResults target = parseMediaRange(mimeType);

    for (ParseResults range : parsedRanges) {
        if ((target.type.equals(range.type) || range.type.equals("*") || target.type.equals("*"))
                && (target.subType.equals(range.subType) || range.subType.equals("*")
                        || target.subType.equals("*"))) {
            for (String k : target.params.keySet()) {
                int paramMatches = 0;

                if (!k.equals("q") && range.params.containsKey(k)
                        && target.params.get(k).equals(range.params.get(k))) {
                    paramMatches++;
                }

                int fitness = (range.type.equals(target.type)) ? 100 : 0;
                fitness += (range.subType.equals(target.subType)) ? 10 : 0;
                fitness += paramMatches;

                if (fitness > bestFitness) {
                    bestFitness = fitness;
                    bestFitQ = NumberUtils.toFloat(range.params.get("q"), 0);
                }
            }
        }
    }

    return new FitnessAndQuality(bestFitness, bestFitQ);
}

From source file:com.netsteadfast.greenstep.bsc.util.AggregationMethod.java

public void averageDateRange(KpiVO kpi, String frequency) throws Exception {
    BscReportSupportUtils.loadExpression();
    for (DateRangeScoreVO dateScore : kpi.getDateRangeScores()) {
        float score = 0.0f;
        int size = 0;
        for (BbMeasureData measureData : kpi.getMeasureDatas()) {
            String date = dateScore.getDate().replaceAll("/", "");
            if (!this.isDateRange(date, frequency, measureData)) {
                continue;
            }//from ww w  .  j a  v a2 s .c  o m
            BscMeasureData data = new BscMeasureData();
            data.setActual(measureData.getActual());
            data.setTarget(measureData.getTarget());
            try {
                Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
                if (value == null) {
                    continue;
                }
                if (!NumberUtils.isNumber(String.valueOf(value))) {
                    continue;
                }
                score += NumberUtils.toFloat(String.valueOf(value), 0.0f);
                size++;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (score != 0.0f && size > 0) {
            score = score / size;
        }
        dateScore.setScore(score);
        dateScore.setFontColor(BscScoreColorUtils.getFontColor(score));
        dateScore.setBgColor(BscScoreColorUtils.getBackgroundColor(score));
        dateScore.setImgIcon(BscReportSupportUtils.getHtmlIcon(kpi, score));
    }
}

From source file:net.gtaun.shoebill.util.config.AbstractConfiguration.java

@Override
public List<Float> getFloatList(String path, List<Float> def) {
    List<?> raw = getList(path);
    if (raw == null)
        return (def != null) ? def : new ArrayList<>();

    return raw.stream().map(o -> NumberUtils.toFloat(o.toString(), 0.0f)).collect(Collectors.toList());
}

From source file:io.wcm.sling.commons.request.RequestParam.java

/**
 * Returns a request parameter as float.
 * @param request Request./*from w  w  w .  j  av a2  s  . c o m*/
 * @param param Parameter name.
 * @param defaultValue Default value.
 * @return Parameter value or default value if it does not exist or is not a number.
 */
public static float getFloat(ServletRequest request, String param, float defaultValue) {
    String value = request.getParameter(param);
    return NumberUtils.toFloat(value, defaultValue);
}

From source file:com.netsteadfast.greenstep.bsc.util.AggregationMethod.java

public float averageDistinct(KpiVO kpi) throws Exception {
    List<BbMeasureData> measureDatas = kpi.getMeasureDatas();
    List<Float> scores = new ArrayList<Float>();
    float score = 0.0f; // init zero
    int size = 0;
    for (BbMeasureData measureData : measureDatas) {
        BscMeasureData data = new BscMeasureData();
        data.setActual(measureData.getActual());
        data.setTarget(measureData.getTarget());
        try {//from   w  w w . j  a  v  a  2 s .  co m
            Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
            if (value == null) {
                continue;
            }
            if (!NumberUtils.isNumber(String.valueOf(value))) {
                continue;
            }
            float nowScore = NumberUtils.toFloat(String.valueOf(value), 0.0f);
            if (!scores.contains(nowScore)) {
                scores.add(nowScore);
                score += nowScore;
                size++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (score != 0.0f && size > 0) {
        score = score / size;
    }
    return score;
}

From source file:com.moviejukebox.tools.PropertiesUtil.java

/**
 * Convert the value to a Float/*from   w  w w  .j a  v a  2  s .  com*/
 *
 * @param key
 * @param valueToConvert
 * @param defaultValue
 * @return
 */
private static float convertFloatProperty(String valueToConvert, float defaultValue) {
    return NumberUtils.toFloat(StringUtils.trimToEmpty(valueToConvert), defaultValue);
}

From source file:ezbake.common.properties.EzProperties.java

/**
 * Get a property as a float, if the property doesn't exist or can't be converted then we return the default value
 *
 * @param propertyName is the name of the property we are looking for (the key)
 * @param defaultValue is the value to return if the key doesn't exist or the value can't be converted to a float
 *
 * @return either the properly parsed float or the default value if the key doesn't exist or can't be converted
 *//*  ww w . j  ava 2s  .co m*/
public float getFloat(String propertyName, float defaultValue) {
    return NumberUtils.toFloat(getProperty(propertyName), defaultValue);
}