Example usage for java.math BigDecimal ROUND_FLOOR

List of usage examples for java.math BigDecimal ROUND_FLOOR

Introduction

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

Prototype

int ROUND_FLOOR

To view the source code for java.math BigDecimal ROUND_FLOOR.

Click Source Link

Document

Rounding mode to round towards negative infinity.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("40");

    BigDecimal bg2 = new BigDecimal("3");

    BigDecimal bg3 = bg1.divide(bg2, BigDecimal.ROUND_FLOOR);

    System.out.println(bg3);//w w w.  j  a  va2 s.  c om
}

From source file:Main.java

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_FLOOR);
    return bd.floatValue();
}

From source file:com.algoTrader.util.RoundUtil.java

public static BigDecimal roundStockOptionStrikeToNextN(BigDecimal spot, double n, OptionType type) {

    if (OptionType.CALL.equals(type)) {
        // increase by strikeOffset and round to upper n
        return roundToNextN(spot, n, BigDecimal.ROUND_CEILING);
    } else {//w  ww . java  2 s . c  o  m
        // reduce by strikeOffset and round to lower n
        return roundToNextN(spot, n, BigDecimal.ROUND_FLOOR);
    }
}

From source file:Money.java

/**
 * returns Money .  If roundCeiling is true we rounded up to
 * the nearest cent, otherwise we round down.  Note that rounding toward the ceiling
 * always rounds to positive infinity (so, for example, -$0.031 becomes
 * -$0.03).   When roundCeiling is false we round toward the floor, so in that case
 * -$0.031 becomes-$0.04.  This is exactly as BigDecimal.ROUND_CEILING and
 * BigDecimal.ROUND_FLOOR behave.//from w w w  .  j  a va  2s. co m
 * @see java.math.BigDecimal.ROUND_CEILING
 */
public Money multiplyAndRound(BigDecimal val, boolean roundCeiling) {
    BigDecimal product = delegate.multiply(val);
    int rounding = roundCeiling ? BigDecimal.ROUND_CEILING : BigDecimal.ROUND_FLOOR;
    return new Money(product.setScale(2, rounding));
}

From source file:resources.common.MathUtilities.java

/**
 * Converts seconds to a whole hour./*  w  w w.j a  v  a2 s .co m*/
 * @author Waverunner
 * @param seconds
 * @return hour(s)
 */
public static int secondsToWholeHours(int seconds) {
    BigDecimal dec = new BigDecimal(seconds);

    return dec.divide(new BigDecimal(3600), BigDecimal.ROUND_FLOOR).intValue();
}

From source file:resources.common.MathUtilities.java

/**
 * Converts seconds to minutes of the hour.
 * @author Waverunner/*from   w  ww .j  a  v a 2 s.c  om*/
 * @param seconds
 * @return hour(s)
 */
public static int secondsToHourMinutes(int seconds) {
    BigDecimal dec = new BigDecimal(seconds);
    return dec.remainder(new BigDecimal(3600)).divide(new BigDecimal(60), BigDecimal.ROUND_FLOOR).intValue();
}

From source file:ch.algotrader.service.algo.VariableIncrementalOrderService.java

@Override
protected VariableIncrementalOrderStateVO handleValidateOrder(final VariableIncrementalOrder algoOrder) {
    Security security = algoOrder.getSecurity();
    SecurityFamily family = security.getSecurityFamily();

    TickVO tick = (TickVO) this.marketDataCacheService.getCurrentMarketDataEvent(security.getId());

    double bidDouble = tick.getBid().doubleValue();
    double askDouble = tick.getAsk().doubleValue();
    double spread = askDouble - bidDouble;
    double increment = algoOrder.getIncrement() * spread;

    double limit;
    double maxLimit;
    if (Side.BUY.equals(algoOrder.getSide())) {
        double limitRaw = bidDouble + algoOrder.getStartOffsetPct() * spread;
        double maxLimitRaw = bidDouble + algoOrder.getEndOffsetPct() * spread;
        limit = RoundUtil.roundToNextN(limitRaw, family.getTickSize(null, limitRaw, true),
                BigDecimal.ROUND_FLOOR);
        maxLimit = RoundUtil.roundToNextN(maxLimitRaw, family.getTickSize(null, maxLimitRaw, true),
                BigDecimal.ROUND_CEILING);
    } else {/*from   w w w.j  a  v  a 2 s  .  co m*/
        double limitRaw = askDouble - algoOrder.getStartOffsetPct() * spread;
        double maxLimitRaw = askDouble - algoOrder.getEndOffsetPct() * spread;
        limit = RoundUtil.roundToNextN(limitRaw, family.getTickSize(null, limitRaw, true),
                BigDecimal.ROUND_CEILING);
        maxLimit = RoundUtil.roundToNextN(maxLimitRaw, family.getTickSize(null, maxLimitRaw, true),
                BigDecimal.ROUND_FLOOR);
    }

    // limit and maxLimit are correctly rounded according to tickSizePattern
    BigDecimal startLimit = RoundUtil.getBigDecimal(limit, family.getScale(null));
    BigDecimal endLimit = RoundUtil.getBigDecimal(maxLimit, family.getScale(null));

    return new VariableIncrementalOrderStateVO(startLimit, endLimit, startLimit, increment);
}

From source file:com.zhihu.android.app.mirror.widget.ArtboardView.java

public boolean isScaling() {
    float scale;//from   w ww . ja v  a  2 s  . c  o m
    float minScale;
    try {
        BigDecimal decimal = new BigDecimal(getScale());
        scale = decimal.setScale(2, BigDecimal.ROUND_FLOOR).floatValue();
        decimal = new BigDecimal(getMinScale());
        minScale = decimal.setScale(2, BigDecimal.ROUND_FLOOR).floatValue();
        return scale > minScale;
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.loadsensing.app.Chart.java

private void generaChart(WebView googleChartView, int checkedId, String SensorSelected) {
    SharedPreferences settings = getSharedPreferences("LoadSensingApp", Context.MODE_PRIVATE);
    String address = SERVER_HOST + "?session=" + settings.getString("session", "") + "&id=" + SensorSelected
            + "&TipusGrafic=" + checkedId;

    ArrayList<HashMap<String, String>> valorsURL = new ArrayList<HashMap<String, String>>();

    try {//from  ww w.j a  v  a 2s.  c  o m
        String jsonString = JsonClient.connectString(address);

        // Convertim la resposta string a un JSONArray
        JSONArray ValorsGrafica = new JSONArray(jsonString);

        // En esta llamada slo hay 1 objeto
        JSONObject Valors = new JSONObject();
        Valors = ValorsGrafica.getJSONObject(0);

        String grafica = Valors.getString("ValorsGrafica");

        // Sobre el string de ValorGrafica, volvemos a parsearlo
        JSONArray ValorGrafica = new JSONArray(grafica);
        for (int i = 0; i < ValorGrafica.length(); i++) {
            JSONObject Valor = new JSONObject();
            Valor = ValorGrafica.getJSONObject(i);

            HashMap<String, String> valorHashMap = new HashMap<String, String>();
            valorHashMap.put("date", Valor.getString("date"));
            valorHashMap.put("value", Valor.getString("value"));
            valorsURL.add(valorHashMap);
        }

    } catch (Exception e) {
        Log.d(DEB_TAG, "Error rebent xarxes");
    }

    // Montamos URL
    String mUrl = CHART_URL;

    // Etiquetas eje X, columna 0 y 1
    mUrl = mUrl + "chxl=0:";
    for (int i = 3; i < valorsURL.size(); i += 4) {
        mUrl = mUrl + "|" + URLEncoder.encode(valorsURL.get(i).get("date"));
    }
    mUrl = mUrl + "|Fecha";
    mUrl = mUrl + "|1:";
    for (int i = 1; i < valorsURL.size(); i += 4) {
        mUrl = mUrl + "|" + URLEncoder.encode(valorsURL.get(i).get("date"));
    }

    // Posicin etiquetas eje Y
    mUrl = mUrl + "&chxp=0,30,70,110|1,10,50,90";

    // Rango x,y

    // Coger valor mnimo y mximo
    float max = new Float(valorsURL.get(0).get("value"));
    float min = new Float(valorsURL.get(0).get("value"));
    for (int i = 1; i < valorsURL.size(); i++) {
        Float valueFloat = new Float(valorsURL.get(i).get("value"));
        max = Math.max(max, valueFloat);
        min = Math.min(min, valueFloat);
    }

    BigDecimal maxRounded = new BigDecimal(max);
    maxRounded = maxRounded.setScale(1, BigDecimal.ROUND_CEILING);
    BigDecimal minRounded = new BigDecimal(min);
    minRounded = minRounded.setScale(1, BigDecimal.ROUND_FLOOR);

    mUrl = mUrl + "&chxr=0,-5,110|1,-5,110|2," + minRounded + "," + maxRounded;

    // Ejes visibles
    mUrl = mUrl + "&chxt=x,x,y";

    // Tipo de grfico
    mUrl = mUrl + "&cht=lxy";

    // Medida del grfico
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth() - 170;
    int height = display.getHeight() - 390;
    mUrl = mUrl + "&chs=" + width + "x" + height;

    // Colores
    mUrl = mUrl + "&chco=3072F3";

    // Escala
    mUrl = mUrl + "&chds=0,9," + minRounded + "," + maxRounded;

    // Valores
    mUrl = mUrl + "&chd=t:0";
    for (int i = 1; i < valorsURL.size(); i++) {
        mUrl = mUrl + "," + i;
    }
    mUrl = mUrl + "|";
    mUrl = mUrl + valorsURL.get(0).get("value");
    for (int i = 1; i < valorsURL.size(); i++) {
        mUrl = mUrl + "," + valorsURL.get(i).get("value");
    }

    // Ttulo eyenda
    switch (checkedId) {
    case R.id.radio1:
        mUrl = mUrl + "&chdl=Sensor+strain+(V)&chdlp=b";
        break;
    case R.id.radio2:
        mUrl = mUrl + "&chdl=Excitation+power+(V)&chdlp=b";
        break;
    case R.id.radio3:
        mUrl = mUrl + "&chdl=Counter+(cnts)&chdlp=b";
        break;
    }

    // Estilo de lineas
    mUrl = mUrl + "&chls=2";

    // Mrgenes
    mUrl = mUrl + "&chma=0,5,5,25|5";

    // Marcador
    mUrl = mUrl + "&chm=r,FF0000,0,0,0";

    Log.d(DEB_TAG, "URL Chart " + mUrl);

    googleChartView.loadUrl(mUrl);
}

From source file:fiskinfoo.no.sintef.fiskinfoo.Implementation.FiskInfoUtility.java

/**
 * Truncates a double value to the number of decimals given
 *
 * @param number/* w w  w  . j  a  v  a2s  .c om*/
 *            The number to truncate
 * @param numberofDecimals
 *            Number of decimals of the truncated number
 * @return
 *            The truncated number.
 */
@SuppressWarnings("unused")
public Double truncateDecimal(double number, int numberofDecimals) {
    if (number > 0) {
        return new BigDecimal(String.valueOf(number)).setScale(numberofDecimals, BigDecimal.ROUND_FLOOR)
                .doubleValue();
    } else {
        return new BigDecimal(String.valueOf(number)).setScale(numberofDecimals, BigDecimal.ROUND_CEILING)
                .doubleValue();
    }
}