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:net.ceos.project.poi.annotated.bean.MultiTypeObjectBuilder.java

/**
 * Create a MultiTypeObject for tests./* ww  w .ja v  a  2 s  .c  om*/
 * 
 * @return the {@link MultiTypeObject}
 */
public static MultiTypeObject buildMultiTypeObject(int multiplier) {
    MultiTypeObject toValidate = new MultiTypeObject();

    toValidate.setDateAttribute(new Date());
    toValidate.setLocalDateAttribute(LocalDate.now());
    toValidate.setLocalDateTimeAttribute(LocalDateTime.now());
    toValidate.setStringAttribute("some string");
    toValidate.setIntegerAttribute(46 * multiplier);
    toValidate.setDoubleAttribute(Double.valueOf("25.3") * multiplier);
    toValidate.setLongAttribute(Long.valueOf("1234567890") * multiplier);
    toValidate.setBooleanAttribute(Boolean.FALSE);
    /* create sub object Job */
    Job job = new Job();
    job.setJobCode(0005);
    job.setJobFamily("Family Job Name");
    job.setJobName("Job Name");
    toValidate.setJob(job);
    toValidate.setIntegerPrimitiveAttribute(121 * multiplier);
    toValidate.setDoublePrimitiveAttribute(44.6 * multiplier);
    toValidate.setLongPrimitiveAttribute(987654321L * multiplier);
    toValidate.setBooleanPrimitiveAttribute(true);
    /* create sub object AddressInfo */
    AddressInfo ai = new AddressInfo();
    ai.setAddress("this is the street");
    ai.setNumber(99);
    ai.setCity("this is the city");
    ai.setCityCode(70065);
    ai.setCountry("This is a Country");
    toValidate.setAddressInfo(ai);
    toValidate.setFloatAttribute(14.765f * multiplier);
    toValidate.setFloatPrimitiveAttribute(11.1125f * multiplier);
    toValidate.setUnitFamily(UnitFamily.COMPONENTS);
    toValidate.setBigDecimalAttribute(BigDecimal.valueOf(24.777).multiply(BigDecimal.valueOf(multiplier)));
    toValidate.setShortAttribute((short) 17);
    toValidate.setShortPrimitiveAttribute((short) 4);
    // TODO add new fields below

    return toValidate;
}

From source file:net.eusashead.hateoas.hal.response.impl.HalResponseBuilderImplTest.java

@Test
public void testSetRepresentation() throws Exception {

    // Build a Representation with the factory
    Representation representation = representationFactory.newRepresentation("/order/123")
            .withProperty("string", "String value").withProperty("date", new Date(123456789l))
            .withProperty("int", 34).withProperty("decimal", BigDecimal.valueOf(123.32d))
            .withProperty("boolean", true).withRepresentation("customer", representationFactory
                    .newRepresentation("/customer").withProperty("email", "test@domain.com"));

    // Create a HalGetResponseBuilder
    HalResponseBuilderImpl builder = new HalResponseBuilderImpl(representationFactory, request);

    // Build the response with the existing Representation
    ResponseEntity<Representation> response = builder.representation(representation).etag(MODIFIED_DATE)
            .lastModified(MODIFIED_DATE).expireIn(1000000).build();

    // Check the headers
    assertHeaders(response);// w w w. ja  v a 2 s.  c  om

    // Check the body
    Assert.assertEquals("/order/123", response.getBody().getLinkByRel("self").getHref());
    Assert.assertEquals("String value", response.getBody().getValue("string").toString());
    Assert.assertEquals(new Date(123456789l).toString(), response.getBody().getValue("date").toString());
    Assert.assertEquals("34", response.getBody().getValue("int").toString());
    Assert.assertEquals("123.32", response.getBody().getValue("decimal").toString());
    Assert.assertEquals("true", response.getBody().getValue("boolean").toString());
    List<? extends ReadableRepresentation> customer = response.getBody().getResourcesByRel("customer");
    Assert.assertNotNull(customer);
    Assert.assertEquals(Integer.valueOf(1), Integer.valueOf(customer.size()));
    ReadableRepresentation cust = customer.get(0);
    Assert.assertEquals("test@domain.com", cust.getValue("email").toString());
}

From source file:dk.clanie.bitcoin.client.response.ListAddressGroupingsResult.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@JsonCreator/*w w  w . j a  v  a 2 s .c  o  m*/
private ListAddressGroupingsResult(Object[] objects) {
    this.objects = objects;
    Map<String, BalanceAndAccount> map = newHashMap();
    for (Object object : objects) {
        List oa = (List) object;
        String address = (String) oa.get(0);
        BigDecimal amount = BigDecimal.valueOf((Double) oa.get(1)).setScale(BitcoindClient.SCALE);
        oa.set(1, amount); // Replace the Double with the BigDecimal to make Jackson serialize with right number of decimal places.
        String account = ((oa.size() > 2) ? (String) oa.get(2) : null);
        map.put(address, new BalanceAndAccount(amount, account));
    }
    balanceAndAmountPerAddress = Collections.unmodifiableMap(map);
}

From source file:com.platform.camel.aggregating_messages.AggregatingMessagesRouteSpringTest.java

@Test
public void aggregatesMessagesByCorrelationKey() throws Exception {
    mockAggregated.expectedHeaderValuesReceivedInAnyOrder("invoiceItemTotal", BigDecimal.valueOf(5),
            BigDecimal.valueOf(4));
    mockAggregated.expectedMessageCount(2);

    start.sendBodyAndHeaders(null,//from  www . j  a  v a  2s  . co  m
            toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(2)));
    start.sendBodyAndHeaders(null,
            toHeadersMap("invoiceId", "invoiceTwo", "invoiceItemTotal", BigDecimal.valueOf(4)));
    start.sendBodyAndHeaders(null,
            toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(3)));
    assertMockEndpointsSatisfied();
}

From source file:com.vladmihalcea.mongo.dao.ProductRepositoryIT.java

@Test
public void testSave() {
    Product product = new Product();
    product.setId(123L);/*from  w  ww  .  ja v  a  2 s  .c  o m*/
    product.setName("Tv");
    product.setQuantity(BigInteger.TEN);
    product.setDiscount(BigDecimal.valueOf(12.34));
    productRepository.save(product);
    Product savedProduct = productRepository.findOne(123L);
    assertEquals(savedProduct, product);
    assertEquals(savedProduct.hashCode(), product.hashCode());
    assertEquals(Long.valueOf(0), product.getVersion());
    assertEquals("Tv", product.getName());
    savedProduct.setName("Dvd");
    savedProduct = productRepository.save(savedProduct);
    assertEquals(Long.valueOf(1), savedProduct.getVersion());
    savedProduct.setVersion(0L);
    try {
        productRepository.save(savedProduct);
        fail("Expected OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException e) {
    }
    productRepository.delete(product);
}

From source file:org.wicketstuff.gmap.api.GLatLng.java

public String getArguments() {

    return new StringBuilder().append(BigDecimal.valueOf(lat).toString()).append(",")
            .append(BigDecimal.valueOf(lng).toString()).append(",").append(Boolean.valueOf(unbounded))
            .toString();//  ww w.  jav a 2  s  .co m
}

From source file:com.sdl.odata.renderer.json.util.JsonWriterUtilTest.java

@Test
public void testWritePrimitiveValues() throws Exception {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stream, JsonEncoding.UTF8);

    jsonGenerator.writeStartObject();/*from w w  w .  j  av a 2  s.  c o m*/
    appendPrimitiveValue("MyString", "Some text", jsonGenerator);
    appendPrimitiveValue("MyByteProperty", Byte.MAX_VALUE, jsonGenerator);
    appendPrimitiveValue("MyShortProperty", (short) 1, jsonGenerator);
    appendPrimitiveValue("MyIntegerProperty", 2, jsonGenerator);
    appendPrimitiveValue("MyFloatProperty", 3.0f, jsonGenerator);
    appendPrimitiveValue("MyDoubleProperty", 4.0d, jsonGenerator);
    appendPrimitiveValue("MyLongProperty", (long) 5, jsonGenerator);
    appendPrimitiveValue("MyBooleanProperty", true, jsonGenerator);
    appendPrimitiveValue("MyUUIDProperty", UUID.fromString("23492a5b-c4f1-4a50-b7a5-d8ebd6067902"),
            jsonGenerator);
    appendPrimitiveValue("DecimalValueProperty", BigDecimal.valueOf(21), jsonGenerator);

    jsonGenerator.writeEndObject();
    jsonGenerator.close();

    assertEquals(prettyPrintJson(readContent(EXPECTED_PRIMITIVE_VALUES_PATH)),
            prettyPrintJson(stream.toString()));
}

From source file:fr.landel.utils.commons.NumberUtils.java

/**
 * Get the max decimal length../*from  www. j  a  v a 2s.  c om*/
 * 
 * @param num1
 *            float
 * @param num2
 *            float
 * @return the max decimal length
 */
private static Integer getMaxDecimalsLength(final Float num1, final Float num2) {
    return Math.max(BigDecimal.valueOf(num1).scale(), BigDecimal.valueOf(num2).scale());
}

From source file:com.khartec.waltz.jobs.sample.AssetCostGenerator.java

private static BigDecimal generateAmount(long mean) {
    double z = mean / 3.4;
    double val = rnd.nextGaussian() * z + mean;

    return BigDecimal.valueOf(val).setScale(2, RoundingMode.CEILING);
}

From source file:cz.muni.fi.pa165.legomanager.dao.LegoKitDaoImplTest.java

@Test(expected = DataAccessException.class)
public void addLegoNameNullTest() {
    LegoKit legoKit = createLegoKit(null, BigDecimal.valueOf(42), 12, new HashSet<Category>(),
            new ArrayList<LegoPiece>(), new ArrayList<LegoSet>());
    legoKitDao.addLegoKit(legoKit);//from   w w w . java 2s .  com
}