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.bgg.model.Rating.java

public void setValue(String value) {
    this.value = NumberUtils.toFloat(value, -1F);
}

From source file:com.jernejerin.traffic.helper.TripOperations.java

/**
 * Parses and validates a trip for erroneous values. It first checks, if parsed string contains
 * 17 values. If it does not, it returns null.
 *
 * If the value is considered erroneous, it is set to the following values:
 *  - primitive types: MIN_VALUE// www .j a  v  a  2 s .  c  o  m
 *  - objects: null
 *
 * @param tripValues comma delimited string representing Trip to check
 * @param timestampReceived Timestamp when the event was received
 * @param id id of the event received
 * @return a Trip with erroneous values set to MIN_VALUE, or null if whole trip was malformed
 */
public static Trip parseValidateTrip(String tripValues, long timestampReceived, int id) {
    //        LOGGER.log(Level.INFO, "Started parsing and validating trip = " +
    //                tripValues + " from thread = " + Thread.currentThread());

    // our returned trip
    Trip trip = new Trip();

    // values are comma separated
    String[] tripSplit = tripValues.split(",");

    // if we do not have 17 values, then return null
    if (tripSplit.length != 17)
        return null;

    // check for correct values and then set them
    trip.setId(id);
    trip.setMedallion(tryParseMD5(tripSplit[0], null));
    trip.setHackLicense(tryParseMD5(tripSplit[1], null));
    trip.setPickupDatetime(tryParseDateTime(tripSplit[2], null));
    trip.setDropOffDatetime(tryParseDateTime(tripSplit[3], null));
    trip.setDropOffTimestamp(
            trip.getDropOffDatetime() != null ? trip.getDropOffDatetime().toEpochSecond(ZoneOffset.UTC) * 1000
                    : 0);
    trip.setTripTime(NumberUtils.toInt(tripSplit[4], Integer.MIN_VALUE));
    trip.setTripDistance(NumberUtils.toFloat(tripSplit[5], Integer.MIN_VALUE));
    trip.setPickupLongitude(tryLongitude(tripSplit[6], Float.MIN_VALUE));
    trip.setPickupLatitude(tryLatitude(tripSplit[7], Float.MIN_VALUE));
    trip.setDropOffLongitude(tryLongitude(tripSplit[8], Float.MIN_VALUE));
    trip.setDropOffLatitude(tryLatitude(tripSplit[9], Float.MIN_VALUE));
    trip.setPaymentType(tryPayment(tripSplit[10], null));
    trip.setFareAmount(NumberUtils.toFloat(tripSplit[11], Float.MIN_VALUE));
    trip.setSurcharge(NumberUtils.toFloat(tripSplit[12], Float.MIN_VALUE));
    trip.setMtaTax(NumberUtils.toFloat(tripSplit[13], Float.MIN_VALUE));
    trip.setTipAmount(NumberUtils.toFloat(tripSplit[14], Float.MIN_VALUE));
    trip.setTollsAmount(NumberUtils.toFloat(tripSplit[15], Float.MIN_VALUE));
    trip.setTotalAmount(NumberUtils.toFloat(tripSplit[16], Float.MIN_VALUE));
    trip.setTimestampReceived(timestampReceived);

    // does the coordinate for pickup location lie inside grid
    if (Cell.inGrid(trip.getPickupLatitude(), trip.getPickupLongitude())
            && Cell.inGrid(trip.getDropOffLatitude(), trip.getDropOffLongitude())) {
        trip.setRoute250(new Route(new Cell250(trip.getPickupLatitude(), trip.getPickupLongitude()),
                new Cell250(trip.getDropOffLatitude(), trip.getDropOffLongitude())));
        trip.setRoute500(new Route(new Cell500(trip.getPickupLatitude(), trip.getPickupLongitude()),
                new Cell500(trip.getDropOffLatitude(), trip.getDropOffLongitude())));
    }

    //        LOGGER.log(Level.INFO, "Finished parsing and validating trip = " +
    //                trip.toString() + " from thread = " + Thread.currentThread());
    return trip;
}

From source file:com.omertron.bgg.model.Rank.java

public void setBayesAverage(String bayesAverage) {
    this.bayesAverage = NumberUtils.toFloat(bayesAverage, -1F);
}

From source file:info.donsun.core.utils.Values.java

/**
 * ???/*w w w  .j av  a2s. c om*/
 * 
 * @param obj 
 * @param defaultValue null
 * @return ??
 */
public static float getFloat(Object obj, float defaultValue) {
    return NumberUtils.toFloat(getString(obj), defaultValue);
}

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

@Override
public float getFloat(String path, float def) {
    Object obj = get(path);//  w w w  .  j  a  v  a2s . c o  m
    if (obj instanceof Float)
        return (Float) obj;
    return NumberUtils.toFloat(obj.toString(), def);
}

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

/**
 * Carves up a media range and returns a ParseResults.
 * /*from  www .j a v a2 s.  com*/
 * For example, the media range 'application/*;q=0.5' would get parsed into:
 * 
 * ('application', '*', {'q', '0.5'})
 * 
 * In addition this function also guarantees that there is a value for 'q'
 * in the params dictionary, filling it in with a proper default if
 * necessary.
 * 
 * @param range
 *            The media range
 */
protected static ParseResults parseMediaRange(String range) {
    ParseResults results = parseMimeType(range);
    String q = results.params.get("q");
    float f = NumberUtils.toFloat(q, 1);

    if (StringUtils.isBlank(q) || f < 0 || f > 1) {
        results.params.put("q", "1");
    }

    return results;
}

From source file:com.omertron.thetvdbapi.model.Banner.java

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

From source file:com.netsteadfast.greenstep.bsc.action.WeightSaveOrUpdateAction.java

@SuppressWarnings("unchecked")
private <T extends BaseValueObj> List<T> fillDatas(Class<?> clazz, String inputHeanId) throws Exception {
    List<T> elements = new ArrayList<T>();
    Enumeration<String> parameterNames = this.getHttpServletRequest().getParameterNames();
    while (parameterNames.hasMoreElements()) {
        String name = parameterNames.nextElement();
        if (!name.startsWith(inputHeanId)) {
            continue;
        }/*from www  . j a va  2s  .c o m*/
        Float value = NumberUtils.toFloat(this.getHttpServletRequest().getParameter(name), 1f);
        T obj = (T) clazz.newInstance();
        Ognl.setValue("oid", obj, name.substring(inputHeanId.length(), name.length()));
        Ognl.setValue("weight", obj, new BigDecimal(value));
        elements.add(obj);
    }
    return elements;
}

From source file:com.netsteadfast.greenstep.bsc.command.PersonalAndOrganizationReportDateRangeScoreCommand.java

private void setDateRangeScore(KpiVO kpi, String dateType, String year) throws Exception {
    float score = 0.0f;
    String date = year + "0101"; // year
    if ("2".equals(dateType)) { // second helf-year
        date = year + "0701";
    }/*from w w w. j av a 2  s .c o m*/
    for (BbMeasureData measureData : kpi.getMeasureDatas()) {
        if (date.equals(measureData.getDate())) {
            BscMeasureData data = new BscMeasureData();
            data.setActual(measureData.getActual());
            data.setTarget(measureData.getTarget());
            Object value = BscFormulaUtils.parse(kpi.getFormula(), data);
            /* 2016-07-01 rem
            if (value != null && (value instanceof Integer || value instanceof Float || value instanceof Long) ) {
               score = NumberUtils.toFloat( String.valueOf(value), 0.0f);
            }
            */
            // 2016-07-01
            if (NumberUtils.isNumber(String.valueOf(value))) {
                score = NumberUtils.toFloat(String.valueOf(value), 0.0f);
            }
        }
    }
    DateRangeScoreVO dateRangeScore = new DateRangeScoreVO();
    dateRangeScore.setDate(date);
    dateRangeScore.setBgColor(BscScoreColorUtils.getBackgroundColor(score));
    dateRangeScore.setFontColor(BscScoreColorUtils.getFontColor(score));
    dateRangeScore.setTarget(kpi.getTarget());
    dateRangeScore.setMin(kpi.getMin());
    dateRangeScore.setScore(score);
    dateRangeScore.setImgIcon("");
    kpi.getDateRangeScores().add(dateRangeScore);
}

From source file:com.o2d.pkayjava.editor.view.ui.properties.panels.UIScenePropertiesMediator.java

@Override
protected void translateViewToItemData() {
    PhysicsPropertiesVO physicsVO = observableReference.physicsPropertiesVO;
    physicsVO.gravityX = NumberUtils.toFloat(viewComponent.getGravityXValue(), physicsVO.gravityX);
    physicsVO.gravityY = NumberUtils.toFloat(viewComponent.getGravityYValue(), physicsVO.gravityY);
    physicsVO.sleepVelocity = NumberUtils.toFloat(viewComponent.getSleepVelocityValue(),
            physicsVO.sleepVelocity);/*from   ww w.j a v a2  s  .  c  om*/
    physicsVO.enabled = viewComponent.isPhysicsEnabled();
    Color color = viewComponent.getAmbientColor();
    observableReference.ambientColor[0] = color.r;
    observableReference.ambientColor[1] = color.g;
    observableReference.ambientColor[2] = color.b;
    observableReference.ambientColor[3] = color.a;

    observableReference.lightSystemEnabled = viewComponent.isLightsEnabled();

    Sandbox.getInstance().setSceneAmbientColor(color, viewComponent.isLightsEnabled());

    Sandbox.getInstance().sceneControl.disableLights(!observableReference.lightSystemEnabled);
    Sandbox.getInstance().sceneControl.disableAmbience(!observableReference.lightSystemEnabled);

    Sandbox.getInstance().sceneControl.setDiffuse(viewComponent.isDiffuse());
}