Example usage for java.math BigDecimal valueOf

List of usage examples for java.math BigDecimal valueOf

Introduction

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

Prototype

public static BigDecimal valueOf(double val) 

Source Link

Document

Translates a double into a BigDecimal , using the double 's canonical string representation provided by the Double#toString(double) method.

Usage

From source file:Main.java

public static double round(double d) {
    if (Double.isNaN(d) || Double.isInfinite(d))
        return d;
    int digits = leadingDigits(d);
    int roundToDecimal = Math.min(MAXDIGIT - digits, MAXDECIMAL);
    double rounded = BigDecimal.valueOf(d).setScale(roundToDecimal, BigDecimal.ROUND_HALF_UP).doubleValue();
    return rounded;
}

From source file:Main.java

public static BigDecimal atan(BigDecimal val) {
    return BigDecimal.valueOf(Math.atan(val.doubleValue()));
}

From source file:Main.java

public static BigDecimal acos(BigDecimal val) {
    return BigDecimal.valueOf(Math.acos(val.doubleValue()));
}

From source file:Main.java

public static BigDecimal asin(BigDecimal val) {
    return BigDecimal.valueOf(Math.asin(val.doubleValue()));
}

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  .j  a v  a2  s  . co m*/
    }
    return x;
}

From source file:Main.java

/**
 * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain
 * precision, BigDecimal objects are used.
 *
 * @param rgb//from  w  w  w  .  ja v a 2  s . com
 * @return
 */
public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) {
    int[] hsb = new int[3];
    int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]);
    int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]);
    int diff = maxChroma - minChroma;

    // Hue
    BigDecimal hue;
    if (diff == 0) {
        hue = BigDecimal.ZERO;
    } else if (maxChroma == rgb[0]) {
        float tmp = (rgb[1] - rgb[2]) / (float) diff;
        if (tmp < 0) {
            tmp += 6 * Math.ceil(-tmp / 6.0);
        } else {
            tmp -= 6 * Math.floor(tmp / 6.0);
        }
        hue = BigDecimal.valueOf(tmp);
    } else if (maxChroma == rgb[1]) {
        hue = BigDecimal.valueOf((rgb[2] - rgb[0]) / (float) diff + 2);
    } else {
        hue = BigDecimal.valueOf((rgb[0] - rgb[1]) / (float) diff + 4);
    }
    // [0, 360] -> [0, 0xffffffff]
    hue = hue.multiply(BigDecimal.valueOf(0xffffffffL));
    hue = hue.divide(BigDecimal.valueOf(6), RoundingMode.FLOOR);
    hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4);

    // Saturation
    if (maxChroma == 0) {
        hsb[1] = 0;
    } else {
        // [0, 1] -> [0, 0xffffffff]
        BigDecimal sat = BigDecimal.valueOf(diff);
        sat = sat.multiply(BigDecimal.valueOf(0xffffffffL));
        sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR);
        hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4);
    }

    // Brightness
    // [0, 255] -> [0, 0xffffffff]
    BigDecimal brightness = BigDecimal.valueOf(maxChroma);
    brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL));
    brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR);
    hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4);

    return hsb;
}

From source file:Main.java

/**
 * Get the given value in satoshis as a string on the form "10.12345000".
 * <p>/* www .j a  va2  s  .co  m*/
 * 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

private static void setterValue(PropertyDescriptor property, Object mapValue, Object object)
        throws InvocationTargetException, IllegalAccessException, ParseException {
    Method setter = property.getWriteMethod();
    if (mapValue == null) {
        setter.invoke(object, mapValue);
        return;/*from w w w .  j  a  va 2 s.  com*/
    }

    Class propertyType = property.getPropertyType();
    String type = propertyType.getName();
    String value = mapValue.toString();

    if (type.equals("java.lang.String")) {
        setter.invoke(object, value);
    } else if (type.equals("java.lang.Integer")) {
        setter.invoke(object, Integer.parseInt(value));
    } else if (type.equals("java.lang.Long")) {
        setter.invoke(object, Long.parseLong(value));
    } else if (type.equals("java.math.BigDecimal")) {
        setter.invoke(object, BigDecimal.valueOf(Double.parseDouble(value)));
    } else if (type.equals("java.math.BigInteger")) {
        setter.invoke(object, BigInteger.valueOf(Long.parseLong(value)));
    } else if (type.equals("java.util.Date")) {
        setter.invoke(object, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value));
    } else if (type.equals("java.lang.Boolean")) {
        setter.invoke(object, Boolean.valueOf(value));
    } else if (type.equals("java.lang.Float")) {
        setter.invoke(object, Float.parseFloat(value));
    } else if (type.equals("java.lang.Double")) {
        setter.invoke(object, Double.parseDouble(value));
    } else if (type.equals("java.lang.byte[]")) {
        setter.invoke(object, value.getBytes());
    } else {
        setter.invoke(object, value);
    }
}

From source file:Main.java

/**
 * Get the given value in satoshis as a string on the form "10.12345".
 * <p>/*from w ww  . j ava 2 s  .  c  o m*/
 * This method only returns necessary decimal points to tell the exact value.
 * If you wish to have all 8 digits use
 * {@link CoinUtil#fullValueString(long)}
 * 
 * @param value
 *           The number of satoshis
 * @return The given value in satoshis as a string on the form "10.12345".
 */
public static String valueString(long value) {
    BigDecimal d = BigDecimal.valueOf(value);
    d = d.divide(ONE_BTC_IN_SATOSHIS);
    return d.toPlainString();
}

From source file:Main.java

/**
 * Compute e^x to a given scale.// ww w . j a v a2 s. 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);
}