Example usage for java.math BigDecimal BigDecimal

List of usage examples for java.math BigDecimal BigDecimal

Introduction

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

Prototype

public BigDecimal(long val) 

Source Link

Document

Translates a long into a BigDecimal .

Usage

From source file:org.castor.jaxb.allannotations.XmlValueTest.java

@Test
public void testFullCycle() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(ForXmlValue.class);
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    ForXmlValue fxv = new ForXmlValue();
    fxv.content = new BigDecimal(4711);
    m.marshal(fxv, sw);/*www.  j a  v a2  s . c  om*/
    System.out.println(sw.toString());
}

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();
    }//  ww w .  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   w w w.j  a v a  2  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:com.mycompany.carshop.test.repository.CustomerInvoiceTest.java

@Test(enabled = true)
public void createInvoice() {
    invoiceRepository = ctx.getBean(CustomerInvoiceRepository.class);

    CustomerInvoice invoice = new CustomerInvoice.Builder("20583").invoiceDate(new Date())
            .orderAmount(new BigDecimal(150.00)).invoiceStatus("Paid").build();

    invoiceRepository.save(invoice);/* w ww  .  j a  v a 2 s . c  o  m*/
    id = invoice.getId();
    Assert.assertNotNull(invoice);
}

From source file:io.wcm.caravan.io.jsontransform.sink.JacksonJsonNodeSinkTest.java

@Test
public void test_simple() throws IOException {
    JacksonJsonNodeSink sink = new JacksonJsonNodeSink(new JsonFactory());
    assertNull(sink.getJsonNode());/*from   ww w.j  ava2s. c  o m*/
    sink.write(JsonElement.DEFAULT_START_OBJECT);
    sink.write(JsonElement.value("key1", "value1"));
    sink.write(JsonElement.startObject("key2"));
    sink.write(JsonElement.value("key3", new BigDecimal("1234.56")));
    sink.write(JsonElement.DEFAULT_END_OBJECT);
    sink.write(JsonElement.startArray("key4"));
    sink.write(JsonElement.value(true));
    sink.write(JsonElement.DEFAULT_START_OBJECT);
    sink.write(JsonElement.value("key5", "value5"));
    sink.write(JsonElement.DEFAULT_END_OBJECT);
    sink.write(JsonElement.DEFAULT_END_ARRAY);
    sink.write(JsonElement.DEFAULT_END_OBJECT);

    JsonNode root = sink.getJsonNode();
    assertEquals("value1", root.get("key1").asText());
    assertEquals(new BigDecimal("1234.56"), root.get("key2").get("key3").decimalValue());
    assertTrue(root.get("key4").get(0).asBoolean());
    assertEquals("value5", root.get("key4").get(1).get("key5").asText());
    sink.close();

}

From source file:com.espertech.esper.epl.agg.aggregator.AggregatorAvgBigDecimal.java

public void clear() {
    sum = new BigDecimal(0.0);
    numDataPoints = 0;
}

From source file:example.orders.Application.java

@PostConstruct
public void init() {

    Customer dave = customers.save(new Customer("Dave", "Matthews", Gender.MALE, //
            new Address("4711 Some Place", "54321", "Charlottesville", "VA")));

    Order order = new Order();

    order.setCustomer(dave);//w w w .  j av  a  2s .c om
    order.add(new LineItem("Lakewood guitar", new BigDecimal(1299.0)));

    orders.save(order);
}

From source file:ch.algotrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value, int scale) {

    if (Double.isNaN(value) || Double.isInfinite(value)) {
        return null;
    } else {/*from  www .ja v a 2  s  .c o  m*/
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }
}

From source file:be.ceau.chart.dataset.PieDataset.java

/**
 * Sets the backing data list to the argument, replacing any data already
 * added or set/* w w  w.  j a va  2  s .  c  om*/
 * 
 * @param data
 *            The data to plot in a line
 */
public PieDataset setData(int... data) {
    clearData();
    if (data != null) {
        for (int i = 0; i < data.length; i++) {
            this.data.add(new BigDecimal(data[i]));
        }
    }
    return this;
}

From source file:mobile.vo.rns.RequireDetailVO.java

public static RequireDetailVO create(Require po) {
    ObjectMapper objectMapper = JackJsonUtil.getMapperInstance(false);
    RequireDetailVO vo = new RequireDetailVO();

    vo.setId(po.getId());// ww  w  . java 2  s  . c  o m

    SkillTag industry = po.getIndustry();
    if (null != industry) {
        vo.setIndustryId(industry.getId());
        vo.setIndustryName(industry.getTagName());
    }

    vo.setTitle(po.getTitle());
    vo.setInfo(po.getInfo());

    if (null == po.getBudget()) {
        vo.setBudget("-1"); // -1 - ?
    } else {
        vo.setBudget(new BigDecimal(po.getBudget()).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
    }

    vo.setCreateDate(new DateTime(po.getCreateDate()).toString("yyyy-MM-dd HH:mm:ss"));

    if (StringUtils.isNotBlank(po.getTags())) {
        try {
            @SuppressWarnings("unchecked")
            List<String> readValue = objectMapper.readValue(po.getTags(), List.class);
            vo.setTags(readValue);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    Iterator<AttachOfRequire> iterator = po.getCaseAttachs().iterator();
    while (iterator.hasNext()) {
        AttachOfRequire attachOfRequire = iterator.next();
        HashMap<String, Object> pair = new HashMap<String, Object>();
        pair.put("attachId", attachOfRequire.id);
        pair.put("filename", attachOfRequire.fileName);
        pair.put("url", Assets.at(attachOfRequire.path));
        vo.getAttachs().add(pair);
    }

    Expert ownerExpert = po.getOwner().getExperts().iterator().next();
    vo.setOwner(User.create(ownerExpert));

    return vo;
}