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

/**
 * Compute x^exponent to a given scale. Uses the same algorithm as class
 * numbercruncher.mathutils.IntPower./*from  w ww.  j  av a  2s . c om*/
 * 
 * @param x
 *            the value x
 * @param exponent
 *            the exponent value
 * @param scale
 *            the desired scale of the result
 * @return the result value
 */
public static BigDecimal intPower(BigDecimal x, long exponent, int scale) {
    // If the exponent is negative, compute 1/(x^-exponent).
    if (exponent < 0) {
        return BigDecimal.valueOf(1).divide(intPower(x, -exponent, scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    BigDecimal power = BigDecimal.valueOf(1);

    // Loop to compute value^exponent.
    while (exponent > 0) {

        // Is the rightmost bit a 1?
        if ((exponent & 1) == 1) {
            power = power.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        }

        // Square x and shift exponent 1 bit to the right.
        x = x.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        exponent >>= 1;

        Thread.yield();
    }

    return power;
}

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;//w  w w .  j a v a 2 s  .  c o m

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }
        i++;
    }

    return currentValue;
}

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;/*www.j av  a 2  s. c  om*/

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }
        i++;
    }

    return currentValue;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;//from w  w  w .  j av a  2s .  co m
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }

        i++;
    }
    return currentValue;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;//from w w w. j a v a 2s  .  c  om
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }

        i++;
    }
    return currentValue;
}

From source file:Main.java

/**
 * Compute e^x to a given scale. Break x into its whole and fraction parts
 * and compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * //from   w  ww  . java 2s .  co  m
 * @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);
}

From source file:com.spring.tutorial.mvc.controller.SampleController.java

@RequestMapping("conversion")
public String conversionSample(Model model) {
    Foo foo = new Foo("This is a bar");
    foo.setDate(new Date());
    foo.setDecimal(BigDecimal.valueOf(10558.45));
    model.addAttribute(foo);/*from   ww w . j a  v  a  2s.c  o m*/
    return "views/conversion";
}

From source file:edu.mum.waa.webstore.repository.impl.InMemoryProdudctRepository.java

public InMemoryProdudctRepository() {
    listOfProducts = new ArrayList<>();
    Product p2 = new Product("iphone123", "iphone 6", BigDecimal.valueOf(600));
    p2.setDescription("This is best");
    p2.setCategory("iphone");
    p2.setManufacturer("apple");
    p2.setUnitsInStock(100);/*  ww  w. j  a v a2s.  c om*/
    listOfProducts.add(p2);
    Product p1 = new Product("iphone123", "iphone 6s", BigDecimal.valueOf(770));
    p1.setDescription("This is best best best");
    p1.setCategory("iphone iphone");
    p1.setManufacturer("apple apple");
    p1.setUnitsInStock(120);
    listOfProducts.add(p1);
}

From source file:edu.mum.waa.webstore.repository.impl.InMemoryProductRepository.java

public InMemoryProductRepository() {
    listOfProduct = new ArrayList<>();

    Product iPhone = new Product("P1234", "iPhone", BigDecimal.valueOf(499));
    iPhone.setDescription("iPhone 7");
    iPhone.setCategory("Smart Phone");
    iPhone.setManufacturer("Apple");
    iPhone.setUnitsInStock(100);/*w ww  .  j  av  a  2 s  .c om*/

    Product nexus7 = new Product("P1235", "Nexus 7", BigDecimal.valueOf(350));
    nexus7.setDescription("Nexus 7 tablet");
    nexus7.setCategory("Tablet");
    nexus7.setManufacturer("Asus");
    nexus7.setUnitsInStock(500);

    Product dellLatitude = new Product("P1236", "Dell Latitude", BigDecimal.valueOf(700));
    dellLatitude.setDescription("Dell latitude laptop");
    dellLatitude.setCategory("Laptop");
    dellLatitude.setManufacturer("Dell");
    dellLatitude.setUnitsInStock(50);

    listOfProduct.add(iPhone);
    listOfProduct.add(nexus7);
    listOfProduct.add(dellLatitude);

}

From source file:de.tsystems.mms.apm.performancesignature.util.PerfSigUIUtils.java

public static BigDecimal round(final double d, final int scale) {
    try {//  w  w  w  . j a v a  2s. co  m
        return BigDecimal.valueOf(d).setScale(d % 1 == 0 ? 0 : scale, BigDecimal.ROUND_HALF_UP);
    } catch (NumberFormatException ex) {
        if (Double.isInfinite(d)) {
            return BigDecimal.valueOf(d);
        } else {
            return BigDecimal.ZERO;
        }
    }
}