Example usage for java.math BigDecimal ROUND_DOWN

List of usage examples for java.math BigDecimal ROUND_DOWN

Introduction

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

Prototype

int ROUND_DOWN

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

Click Source Link

Document

Rounding mode to round towards zero.

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_DOWN);

    System.out.println(bg3);//www. j a va 2 s.  com
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int decimalPlaces = 2;
    BigDecimal bd = new BigDecimal("123456789.0123456890");

    bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
    String string = bd.toString();
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("16");
    BigDecimal bg2 = new BigDecimal("3");

    // divide bg1 with bg2 with 4 scale
    // 1 specifies BigDecimal.ROUND_DOWN
    BigDecimal bg3 = bg1.divide(bg2, 4, BigDecimal.ROUND_DOWN);

    System.out.println(bg3);/*from  w ww . j a  v a  2 s  .  c om*/
}

From source file:Main.java

public static int truncateDoubleToInt(double d) {
    return new BigDecimal(d).setScale(0, BigDecimal.ROUND_DOWN).intValue();
}

From source file:Main.java

public static String getDecimalPrice(float price) {
    BigDecimal bigDecimal = new BigDecimal(price);
    return bigDecimal.setScale(1, BigDecimal.ROUND_DOWN).toString();
}

From source file:Main.java

public static int getNumericByDouble(double value) {
    BigDecimal valueBigDecimal = new BigDecimal(value);
    return valueBigDecimal.setScale(0, BigDecimal.ROUND_DOWN).intValue();
}

From source file:Main.java

public static String getMoneyDisplay(BigDecimal amount) {
    if (amount == null) {
        return "0.00";
    } else {/*from   w  w w. ja va  2s  .c  o m*/
        DecimalFormat df = new DecimalFormat("##.00");
        return df.format(amount.setScale(2, BigDecimal.ROUND_DOWN));
    }
}

From source file:Main.java

public static Bitmap compressBitmap(Bitmap bitmap, int width, int height, boolean isAdjust) {
    if (bitmap.getWidth() > width || bitmap.getHeight() > height) {
        float scaleX = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()), 4, BigDecimal.ROUND_DOWN)
                .floatValue();/*from  w  w w .j a v a  2s  .c o m*/
        float scaleY = new BigDecimal(height)
                .divide(new BigDecimal(bitmap.getHeight()), 4, BigDecimal.ROUND_DOWN).floatValue();
        if (isAdjust) {
            scaleX = (scaleX < scaleY ? scaleX : scaleY);
            scaleY = scaleX;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(scaleX, scaleY);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    return bitmap;
}

From source file:BigDSqrt.java

public static BigDecimal sqrt(BigDecimal n, int s) {
    BigDecimal TWO = BigDecimal.valueOf(2);

    // Obtain the first approximation
    BigDecimal x = n.divide(BigDecimal.valueOf(3), s, BigDecimal.ROUND_DOWN);
    BigDecimal lastX = BigDecimal.valueOf(0);

    // Proceed through 50 iterations
    for (int i = 0; i < 50; i++) {
        x = n.add(x.multiply(x)).divide(x.multiply(TWO), s, BigDecimal.ROUND_DOWN);
        if (x.compareTo(lastX) == 0)
            break;
        lastX = x;//from   w w  w.java 2s .c o  m
    }
    return x;
}

From source file:Main.java

/**
 * Compute e^x to a given scale.//from w w w .  j  av a 2s  .  c o m
 * Break x into its whole and fraction parts and
 * compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * @param x the value of x
 * @param scale the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0)
        return expTaylor(x, scale);

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}