Example usage for java.math BigDecimal floatValue

List of usage examples for java.math BigDecimal floatValue

Introduction

In this page you can find the example usage for java.math BigDecimal floatValue.

Prototype

@Override
public float floatValue() 

Source Link

Document

Converts this BigDecimal to a float .

Usage

From source file:model.experiments.tuningRuns.CompetitiveAveragingGridSearch.java

/**
 * Round to certain number of decimals/*from   ww w  .  j a  va 2s  .  com*/
 *
 * @param d
 * @param decimalPlace
 * @return
 */
public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:org.eyeseetea.malariacare.utils.Utils.java

public static String round(float base, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(base));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    if (decimalPlace == 0)
        return Integer.toString((int) bd.floatValue());
    return Float.toString(bd.floatValue());
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Round something with bankers rounding
 * @param d float to round/*from  www .  j  a  v  a  2 s  . co m*/
 * @param decimalPlace places to round to
 * @return new float
 */
public static float round(float d, int decimalPlace, RoundingMode mode) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, mode);

    return bd.floatValue();
}

From source file:com.kcs.core.utilities.Utility.java

public static float getXmlFloat(BigDecimal value) {
    if (isNull(value)) {
        return 0.0f;
    } else {//from  w  w w .j a v  a2  s  . c o  m
        return value.floatValue();
    }
}

From source file:org.psikeds.resolutionengine.datalayer.knowledgebase.util.FeatureValueHelper.java

public static List<FloatFeatureValue> calculateFloatRange(final String featureID, final String rangeID,
        final BigDecimal min, final BigDecimal max, final BigDecimal inc, int scale, final int roundingMode,
        final int maxSize) {
    float finc = (inc == null ? 0.0f : inc.floatValue());
    if (finc == 0.0f) {
        finc = DEFAULT_RANGE_STEP;/*from  w w  w.  j a va  2 s. c  o  m*/
    } else if (scale == FloatFeatureValue.MIN_FLOAT_SCALE) {
        // scale is based on the increment, if not explicitly specified otherwise
        scale = inc.scale();
    }
    final float fmin = (min == null ? 0.0f : min.floatValue());
    final float fmax = (max == null ? 0.0f : max.floatValue());
    if ((finc > 0.0f) && (fmax < fmin)) {
        throw new IllegalArgumentException(
                "Maximum of Range " + rangeID + " must not be smaller than Minimum!");
    }
    if ((finc < 0.0f) && (fmax > fmin)) {
        throw new IllegalArgumentException(
                "Minimum of Range " + rangeID + " must not be smaller than Maximum!");
    }
    final List<FloatFeatureValue> lst = new ArrayList<FloatFeatureValue>();
    int count = 1;
    for (float f = fmin; (f <= fmax) && (count <= maxSize); f = f + finc) {
        final String featureValueID = rangeID + "_F" + String.valueOf(count);
        final FloatFeatureValue ffv = new FloatFeatureValue(featureID, featureValueID, f, scale, roundingMode);
        lst.add(ffv);
        count++;
    }
    return lst;
}

From source file:org.runnerup.export.format.RunKeeper.java

public static ActivityEntity parseToActivity(JSONObject response, double unitMeters) throws JSONException {
    ActivityEntity newActivity = new ActivityEntity();
    newActivity.setSport(RunKeeperSynchronizer.runkeeper2sportMap.get(response.getString("type")).getDbValue());
    if (response.has("notes")) {
        newActivity.setComment(response.getString("notes"));
    }//from ww w.  j a  v a  2s. co  m
    newActivity.setTime((long) Float.parseFloat(response.getString("duration")));
    newActivity.setDistance(Float.parseFloat(response.getString("total_distance")));

    String startTime = response.getString("start_time");
    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
    try {
        newActivity.setStartTime(format.parse(startTime));
    } catch (ParseException e) {
        Log.e(Constants.LOG, e.getMessage());
        return null;
    }

    List<LapEntity> laps = new ArrayList<LapEntity>();
    List<LocationEntity> locations = new ArrayList<LocationEntity>();

    JSONArray distance = response.getJSONArray("distance");
    JSONArray path = response.getJSONArray("path");
    JSONArray hr = response.getJSONArray("heart_rate");

    SortedMap<Long, HashMap<String, String>> pointsValueMap = createPointsMap(distance, path, hr);
    Iterator<Map.Entry<Long, HashMap<String, String>>> points = pointsValueMap.entrySet().iterator();

    //lap hr
    int maxHr = 0;
    int sumHr = 0;
    int count = 0;
    //point speed
    long time = 0;
    float meters = 0.0f;
    //activity hr
    int maxHrOverall = 0;
    int sumHrOverall = 0;
    int countOverall = 0;

    while (points.hasNext()) {
        Map.Entry<Long, HashMap<String, String>> timePoint = points.next();
        HashMap<String, String> values = timePoint.getValue();

        LocationEntity lv = new LocationEntity();
        lv.setActivityId(newActivity.getId());
        lv.setTime(TimeUnit.SECONDS.toMillis(newActivity.getStartTime()) + timePoint.getKey());

        String dist = values.get("distance");
        if (dist == null) {
            continue;
        }
        String lat = values.get("latitude");
        String lon = values.get("longitude");
        String alt = values.get("altitude");
        String heart = values.get("heart_rate");
        String type = values.get("type");

        if (lat == null || lon == null) {
            continue;
        } else {
            lv.setLatitude(Double.valueOf(lat));
            lv.setLongitude(Double.valueOf(lon));
        }
        if (alt != null) {
            lv.setAltitude(Double.valueOf(alt));
        }

        if (pointsValueMap.firstKey().equals(timePoint.getKey())) {
            lv.setType(DB.LOCATION.TYPE_START);
        } else if (!points.hasNext()) {
            lv.setType(DB.LOCATION.TYPE_END);
        } else if (type != null) {
            lv.setType(RunKeeperSynchronizer.POINT_TYPE.get(type));
        }
        // lap and activity max and avg hr
        if (heart != null) {
            lv.setHr(Integer.valueOf(heart));
            maxHr = Math.max(maxHr, lv.getHr());
            maxHrOverall = Math.max(maxHrOverall, lv.getHr());
            sumHr += lv.getHr();
            sumHrOverall += lv.getHr();
            count++;
            countOverall++;
        }

        meters = Float.valueOf(dist) - meters;
        time = timePoint.getKey() - time;
        if (time > 0) {
            float speed = meters / (float) TimeUnit.MILLISECONDS.toSeconds(time);
            BigDecimal s = new BigDecimal(speed);
            s = s.setScale(2, BigDecimal.ROUND_UP);
            lv.setSpeed(s.floatValue());
        }

        // create lap if distance greater than configured lap distance

        if (Float.valueOf(dist) >= unitMeters * laps.size()) {
            LapEntity newLap = new LapEntity();
            newLap.setLap(laps.size());
            newLap.setDistance(Float.valueOf(dist));
            newLap.setTime((int) TimeUnit.MILLISECONDS.toSeconds(timePoint.getKey()));
            newLap.setActivityId(newActivity.getId());
            laps.add(newLap);

            // update previous lap with duration and distance
            if (laps.size() > 1) {
                LapEntity previousLap = laps.get(laps.size() - 2);
                previousLap.setDistance(Float.valueOf(dist) - previousLap.getDistance());
                previousLap.setTime(
                        (int) TimeUnit.MILLISECONDS.toSeconds(timePoint.getKey()) - previousLap.getTime());

                if (hr != null && hr.length() > 0) {
                    previousLap.setMaxHr(maxHr);
                    previousLap.setAvgHr(sumHr / count);
                }
                maxHr = 0;
                sumHr = 0;
                count = 0;
            }
        }
        // update last lap with duration and distance
        if (!points.hasNext()) {
            LapEntity previousLap = laps.get(laps.size() - 1);
            previousLap.setDistance(Float.valueOf(dist) - previousLap.getDistance());
            previousLap
                    .setTime((int) TimeUnit.MILLISECONDS.toSeconds(timePoint.getKey()) - previousLap.getTime());

            if (hr != null && hr.length() > 0) {
                previousLap.setMaxHr(maxHr);
                previousLap.setAvgHr(sumHr / count);
            }
        }

        lv.setLap(laps.size() - 1);

        locations.add(lv);
    }
    // calculate avg and max hr
    // update the activity
    newActivity.setMaxHr(maxHrOverall);
    if (countOverall > 0) {
        newActivity.setAvgHr(sumHrOverall / countOverall);
    }

    newActivity.putPoints(locations);
    newActivity.putLaps(laps);

    return newActivity;
}

From source file:com.github.jessemull.microflex.util.BigDecimalUtil.java

/**
 * Converts a list of BigDecimals to a list of floats.
 * @param    List<BigDecimal>    list of BigDecimals
 * @return                       list of floats
 *///  ww w  .  j  a  v  a  2 s  .  c  o  m
public static List<Float> toFloatList(List<BigDecimal> list) {

    List<Float> floatList = new ArrayList<Float>();

    for (BigDecimal val : list) {
        if (!OverFlowUtil.floatOverflow(val)) {
            OverFlowUtil.overflowError(val);
        }
        floatList.add(val.floatValue());
    }

    return floatList;

}

From source file:org.olat.core.util.Formatter.java

License:asdf

/**
 * Round a float value to a float value with given number of figures after comma
 * //from   w  w  w.j  a v a 2  s.  c o  m
 * @param value
 * @param decimalPlace
 * @return rounded float value
 */
public static float round(float value, int decimalPlace) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    value = bd.floatValue();
    return value;
}

From source file:com.ghy.common.orm.hibernate.HibernateDao.java

/**
 * //from   w  w w.j a  v a  2  s  .c o  m
 * @param sql
 * @param params
 * @return
 */
public Float findBySqlFloat(String sql, Map<String, ?> params) {
    SQLQuery queryObject = getSession().createSQLQuery(sql);
    if (params != null && params.size() > 0)
        queryObject.setProperties(params);
    List<BigDecimal> qlist = queryObject.list();
    if (qlist != null && qlist.size() > 0) {
        BigDecimal obj = qlist.get(0);
        return obj.floatValue();
    }
    return Float.valueOf(0);
}

From source file:com.norconex.collector.http.data.store.impl.jdbc.JDBCCrawlDataSerializer.java

@Override
public ICrawlData toCrawlData(String table, ResultSet rs) throws SQLException {
    if (rs == null) {
        return null;
    }// w w w. j  a  v  a  2 s  .  co  m
    HttpCrawlData data = new HttpCrawlData();
    data.setReference(rs.getString("reference"));
    data.setParentRootReference(rs.getString("parentRootReference"));
    data.setRootParentReference(rs.getBoolean("isRootParentReference"));
    data.setState(HttpCrawlState.valueOf(rs.getString("state")));
    data.setMetaChecksum(rs.getString("metaChecksum"));
    data.setDocumentChecksum(rs.getString("contentChecksum"));
    data.setDepth(rs.getInt("depth"));
    BigDecimal bigLM = rs.getBigDecimal("sitemapLastMod");
    if (bigLM != null) {
        data.setSitemapLastMod(bigLM.longValue());
    }
    BigDecimal bigP = rs.getBigDecimal("sitemapPriority");
    if (bigP != null) {
        data.setSitemapPriority(bigP.floatValue());
    }
    data.setSitemapChangeFreq(rs.getString("sitemapChangeFreq"));
    data.setReferrerReference(rs.getString("referrerReference"));
    data.setReferrerLinkTag(rs.getString("referrerLinkTag"));
    data.setReferrerLinkText(rs.getString("referrerLinkText"));
    data.setReferrerLinkTitle(rs.getString("referrerLinkTitle"));

    return data;
}