Example usage for java.math BigDecimal movePointRight

List of usage examples for java.math BigDecimal movePointRight

Introduction

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

Prototype

public BigDecimal movePointRight(int n) 

Source Link

Document

Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.23");
    BigDecimal bg2 = new BigDecimal("12323");

    BigDecimal bg3 = bg1.movePointRight(3); // 3 places right
    BigDecimal bg4 = bg2.movePointRight(-2);// 2 places left

    System.out.println(bg3);//w ww  . ja  v a  2  s.  c om
    System.out.println(bg4);
}

From source file:Main.java

public static void main(String... args) {
    long base = 12345;
    int scale = 4;

    BigDecimal number = BigDecimal.valueOf(base, scale);
    System.out.println(number);//w  ww  .j  a va2s  .  c  o m
    BigDecimal pointRight = number.movePointRight(5);
    System.out.println(pointRight + "; my scale is " + pointRight.scale());
    BigDecimal scaleBy = number.scaleByPowerOfTen(5);
    System.out.println(scaleBy + "; my scale is " + scaleBy.scale());
}

From source file:Main.java

public static String formatAmountConversionRate(double convRate) {
    if (convRate == 0)
        return null;
    BigDecimal cr = new BigDecimal(convRate);
    int x = 7 - cr.precision() + cr.scale();
    String bds = cr.movePointRight(cr.scale()).toString();
    if (x > 9)
        bds = zeropad(bds, bds.length() + x - 9);
    String ret = zeropadRight(bds, 7);
    return Math.min(9, x) + takeFirstN(ret, 7);
}

From source file:Main.java

/**
 * Compute the square root of x to a given scale, x >= 0. Use Newton's
 * algorithm./*from   w w  w. j  av a 2s  .co  m*/
 * 
 * @param x
 *            the value of x
 * @return the result value
 */
public static BigDecimal sqrt(BigDecimal x) {
    // Check that x >= 0.
    if (x.signum() < 0) {
        throw new ArithmeticException("x < 0");
    }

    // n = x*(10^(2*SCALE))
    BigInteger n = x.movePointRight(SCALE << 1).toBigInteger();

    // The first approximation is the upper half of n.
    int bits = (n.bitLength() + 1) >> 1;
    BigInteger ix = n.shiftRight(bits);
    BigInteger ixPrev;

    // Loop until the approximations converge
    // (two successive approximations are equal after rounding).
    do {
        ixPrev = ix;

        // x = (x + n/x)/2
        ix = ix.add(n.divide(ix)).shiftRight(1);

        Thread.yield();
    } while (ix.compareTo(ixPrev) != 0);

    return new BigDecimal(ix, SCALE);
}

From source file:Main.java

/**
 * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since
 * the epoch.//w  w w  .  ja  va 2s .  co m
 *
 * @param value The attribute value to parse.
 * @return The parsed timestamp in milliseconds since the epoch.
 */
public static long parseXsDateTime(String value) throws ParseException {
    Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value);
    if (!matcher.matches()) {
        throw new ParseException("Invalid date/time format: " + value, 0);
    }

    int timezoneShift;
    if (matcher.group(9) == null) {
        // No time zone specified.
        timezoneShift = 0;
    } else if (matcher.group(9).equalsIgnoreCase("Z")) {
        timezoneShift = 0;
    } else {
        timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13))));
        if (matcher.group(11).equals("-")) {
            timezoneShift *= -1;
        }
    }

    Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

    dateTime.clear();
    // Note: The month value is 0-based, hence the -1 on group(2)
    dateTime.set(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1,
            Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)),
            Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6)));
    if (!TextUtils.isEmpty(matcher.group(8))) {
        final BigDecimal bd = new BigDecimal("0." + matcher.group(8));
        // we care only for milliseconds, so movePointRight(3)
        dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue());
    }

    long time = dateTime.getTimeInMillis();
    if (timezoneShift != 0) {
        time -= timezoneShift * 60000;
    }

    return time;
}

From source file:de.jfachwert.math.Bruch.java

private static Bruch toBruch(BigDecimal decimal) {
    int scale = decimal.scale();
    BigInteger z = decimal.movePointRight(scale).toBigInteger();
    BigInteger n = BigDecimal.ONE.movePointRight(scale).toBigInteger();
    return Bruch.of(z, n).kuerzen();
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static double getLegacyPriceFromMoneyNode(JsonNode moneyNode) {
    BigDecimal price = getAmountFromMoneyNode(moneyNode);
    return price.movePointRight(2).doubleValue();
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java

BigDecimal decodeBinDecimal(ByteBuf cb, int bufferLen, boolean isIntegerPortion) throws PEException {
    BigDecimal decimalPortion = new BigDecimal(0);
    if (bufferLen > 0) {

        ByteBuf decimalPortionBuf = cb.readBytes(bufferLen);

        if (isIntegerPortion) {
            int initialBytes = bufferLen % 4;
            if (initialBytes > 0) {
                long intValue = readValue(decimalPortionBuf, initialBytes);
                decimalPortion = BigDecimal.valueOf(intValue);
            }/*from   w w  w.ja  v  a 2  s .co  m*/
        }

        int decimalPortionLen = decimalPortionBuf.readableBytes();

        while (decimalPortionLen > 0) {
            int nextLen = (decimalPortionLen < 4) ? decimalPortionLen : 4;
            long intValue = readValue(decimalPortionBuf, nextLen);

            if (intValue > 0) {
                if (decimalPortion.longValue() == 0) {
                    decimalPortion = decimalPortion.add(BigDecimal.valueOf(intValue));
                } else {
                    int digits = (int) (Math.log10(intValue) + 1);
                    decimalPortion = decimalPortion.movePointRight(digits).add(BigDecimal.valueOf(intValue));
                }
            }

            decimalPortionLen = decimalPortionBuf.readableBytes();
        }
    }
    return decimalPortion;
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Writes "Currency" values.//from  w  ww  .  j  a  v a2  s .co  m
 */
private static void writeCurrencyValue(ByteBuffer buffer, Object value) throws IOException {
    Object inValue = value;
    try {
        BigDecimal decVal = toBigDecimal(value);
        inValue = decVal;

        // adjust scale (will cause the an ArithmeticException if number has too
        // many decimal places)
        decVal = decVal.setScale(4);

        // now, remove scale and convert to long (this will throw if the value is
        // too big)
        buffer.putLong(decVal.movePointRight(4).longValueExact());
    } catch (ArithmeticException e) {
        throw (IOException) new IOException("Currency value '" + inValue + "' out of range").initCause(e);
    }
}

From source file:org.apache.calcite.runtime.SqlFunctions.java

/** SQL <code>ROUND</code> operator applied to BigDecimal values. */
public static BigDecimal sround(BigDecimal b0, int b1) {
    return b0.movePointRight(b1).setScale(0, RoundingMode.HALF_UP).movePointLeft(b1);
}