Example usage for java.math BigDecimal movePointLeft

List of usage examples for java.math BigDecimal movePointLeft

Introduction

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

Prototype

public BigDecimal movePointLeft(int n) 

Source Link

Document

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

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.movePointLeft(3); // 3 points left
    BigDecimal bg4 = bg2.movePointLeft(-2);// 2 points right

    // print bg3, bg4 values
    System.out.println(bg3);/*from   w  w  w.ja v  a  2s  .c  om*/
    System.out.println(bg4);
}

From source file:Main.java

/**
 * Get the given value in satoshis as a string on the form "10.12345000".
 * <p>/*from   www.jav a2  s .c  om*/
 * This method always returns a string with 8 decimal points. If you only
 * wish to have the necessary digits use {@link CoinUtil#valueString(long)}
 * 
 * @param value
 *           The number of satoshis
 * @return The given value in satoshis as a string on the form "10.12345000".
 */
public static String fullValueString(long value) {
    BigDecimal d = BigDecimal.valueOf(value);
    d = d.movePointLeft(8);
    return d.toPlainString();
}

From source file:Main.java

/**
 * Parse currency amount conversion rate string
 * <p/>/* www .jav  a2  s .  c  om*/
 * Suitble for parse fields 10 and 11
 *
 * @param convRate amount conversation rate
 * @return parsed currency amount conversation rate
 * @throws IllegalArgumentException
 */
public static double parseAmountConversionRate(String convRate) {
    if (convRate == null || convRate.length() != 8)
        throw new IllegalArgumentException("Invalid amount converion rate argument: '" + convRate + "'");
    BigDecimal bd = new BigDecimal(convRate);
    int pow = bd.movePointLeft(7).intValue();
    bd = new BigDecimal(convRate.substring(1));
    return bd.movePointLeft(pow).doubleValue();
}

From source file:Main.java

static void unmarshalIn(org.omg.CORBA.portable.InputStream s, TypeCode typeCode, long[] la, Object[] oa) {
    int type = typeCode.kind().value();
    long l = 0;/*from   www.j  av a2s  .  c  om*/
    Object o = oa[0];

    switch (type) {
    case TCKind._tk_null:
    case TCKind._tk_void:
    case TCKind._tk_native:
        // Nothing to read
        break;

    case TCKind._tk_short:
        l = s.read_short() & 0xFFFFL;
        break;

    case TCKind._tk_ushort:
        l = s.read_ushort() & 0xFFFFL;
        break;

    case TCKind._tk_enum:
    case TCKind._tk_long:
        l = s.read_long() & 0xFFFFFFFFL;
        break;

    case TCKind._tk_ulong:
        l = s.read_ulong() & 0xFFFFFFFFL;
        break;

    case TCKind._tk_float:
        l = Float.floatToIntBits(s.read_float()) & 0xFFFFFFFFL;
        break;

    case TCKind._tk_double:
        l = Double.doubleToLongBits(s.read_double());
        break;

    case TCKind._tk_char:
        l = s.read_char() & 0xFFFFL;
        break;

    case TCKind._tk_octet:
        l = s.read_octet() & 0xFFL;
        break;

    case TCKind._tk_boolean:
        if (s.read_boolean())
            l = 1;
        else
            l = 0;
        break;

    case TCKind._tk_any:
        o = s.read_any();
        break;

    case TCKind._tk_TypeCode:
        o = s.read_TypeCode();
        break;

    case TCKind._tk_Principal:
        o = s.read_Principal();
        break;

    case TCKind._tk_objref:
        if (o instanceof Streamable)
            ((Streamable) o)._read(s);
        else
            o = s.read_Object();
        break;

    case TCKind._tk_longlong:
        l = s.read_longlong();
        break;

    case TCKind._tk_ulonglong:
        l = s.read_ulonglong();
        break;

    case TCKind._tk_wchar:
        l = s.read_wchar() & 0xFFFFL;
        break;

    case TCKind._tk_string:
        o = s.read_string();
        break;

    case TCKind._tk_wstring:
        o = s.read_wstring();
        break;

    case TCKind._tk_value:
    case TCKind._tk_value_box:
        o = ((org.omg.CORBA_2_3.portable.InputStream) s).read_value();
        break;

    case TCKind._tk_fixed:
        try {
            // _REVISIT_ As soon as the java-rtf adds digits and scale parameters to
            // InputStream, this check will be unnecessary
            if (s instanceof CDRInputStream) {
                o = ((CDRInputStream) s).read_fixed(typeCode.fixed_digits(), typeCode.fixed_scale());
            } else {
                BigDecimal bigDecimal = s.read_fixed();
                o = bigDecimal.movePointLeft((int) typeCode.fixed_scale());
            }
        } catch (BadKind badKind) { // impossible
        }
        break;

    case TCKind._tk_struct:
    case TCKind._tk_union:
    case TCKind._tk_sequence:
    case TCKind._tk_array:
    case TCKind._tk_alias:
    case TCKind._tk_except:
        ((Streamable) o)._read(s);
        break;

    case TCKind._tk_abstract_interface:
        o = ((org.omg.CORBA_2_3.portable.InputStream) s).read_abstract_interface();
        break;

    case TCKind._tk_longdouble:
        // Unspecified for Java
    default:
        ORBUtilSystemException wrapper = ORBUtilSystemException.get((com.sun.corba.se.spi.orb.ORB) s.orb(),
                CORBALogDomains.RPC_PRESENTATION);
        throw wrapper.typecodeNotSupported();
    }

    oa[0] = o;
    la[0] = l;
}

From source file:org.multibit.utils.CSMiscUtils.java

@Deprecated
public static BigInteger calcRawPercentageCharge(CSAsset asset, BigInteger transferAmount) {
    CoinSparkGenesis genesis = asset.getGenesis();
    int chargeBasisPoints = genesis.getChargeBasisPoints();
    if (chargeBasisPoints == 0)
        return BigInteger.ZERO;
    BigDecimal d = new BigDecimal(chargeBasisPoints);
    d = d.movePointLeft(2 + 2); // 2 to get whole percent number, another 2 to get fraction
    d = d.multiply(new BigDecimal(transferAmount), MathContext.UNLIMITED);
    return d.toBigInteger();
}

From source file:com.skubit.satoshidice.placebet.PlaceBetFragment.java

private void updateWinField() {
    String text = mWinEdit.getText().toString();
    BigDecimal bd = text.isEmpty() ? new BigDecimal(.002) : new BigDecimal(text.toString());

    BetParameters bp = BetParameters.calculateWithWinChance(bd.movePointLeft(2));
    mSeekbar.setProgress(bp.getNumberToRollUnder());
    setTextFields(bp);/* ww  w. j  a v a  2 s .com*/
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    //  and then add one again to get the next number
    //  so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    //special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }//  w  ww .j  a  va 2 s . co  m

    StringBuilder sb = new StringBuilder();
    //number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    //so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    //this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    //Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    // and then add one again to get the next number
    // so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    // special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }//from  www.  j a  v a2  s.c o m

    StringBuilder sb = new StringBuilder();
    // number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    // so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    // this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    // Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:br.com.nordestefomento.jrimum.utilix.Field.java

@SuppressWarnings("unchecked")
private void readDecimalField(String valueAsString) {

    DecimalFormat decimalFormat = (DecimalFormat) format;

    try {// w  w w  . j av a  2 s.  c o  m

        Long parsedValue = (Long) format.parseObject(valueAsString);
        BigDecimal decimalValue = new BigDecimal(parsedValue.longValue());
        decimalValue = decimalValue.movePointLeft(decimalFormat.getMaximumFractionDigits());

        value = (G) decimalValue;

    } catch (ParseException e) {

        getGenericReadError(e, valueAsString);
    }
}

From source file:org.jrimum.utilix.text.Field.java

/**
 * L campos de valores decimais. O parse  feito atravs de formatadores de nmeros decimais.
 * @param valueAsString//from   w  ww . j a va  2 s. c om
 */
@SuppressWarnings("unchecked")
private void readDecimalField(String valueAsString) {

    DecimalFormat decimalFormat = (DecimalFormat) format;

    try {

        Long parsedValue = (Long) format.parseObject(valueAsString);
        BigDecimal decimalValue = new BigDecimal(parsedValue.longValue());
        decimalValue = decimalValue.movePointLeft(decimalFormat.getMaximumFractionDigits());

        value = (G) decimalValue;

    } catch (ParseException e) {

        getGenericReadError(e, valueAsString);
    }
}