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:com.haulmont.timesheets.global.WorkTimeConfigBean.java

public BigDecimal getUserWorkHourForDay(User user) {
    return getUserWorkHourForWeek(user).divide(BigDecimal.valueOf(getWorkDaysCount()),
            BigDecimal.ROUND_HALF_UP);
}

From source file:edu.usu.sdl.openstorefront.validation.MaxValueRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    Max max = field.getAnnotation(Max.class);
    if (max != null) {
        try {/*from  ww w.jav  a 2 s.c  o  m*/
            String value = BeanUtils.getProperty(dataObject, field.getName());
            if (value != null) {
                try {
                    BigDecimal numberValue = new BigDecimal(value);
                    if (numberValue.compareTo(BigDecimal.valueOf(max.value())) == 1) {
                        valid = false;
                    }
                } catch (NumberFormatException e) {
                    throw new OpenStorefrontRuntimeException("This annotation is for numbers only",
                            "Programming error");
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:edu.usu.sdl.openstorefront.validation.MinValueRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    Min min = field.getAnnotation(Min.class);
    if (min != null) {
        try {/* w  w w . j ava 2s  .  c  om*/
            String value = BeanUtils.getProperty(dataObject, field.getName());
            if (value != null) {
                try {
                    BigDecimal numberValue = new BigDecimal(value);
                    if (numberValue.compareTo(BigDecimal.valueOf(min.value())) == -1) {
                        valid = false;
                    }
                } catch (NumberFormatException e) {
                    throw new OpenStorefrontRuntimeException("This annotation is for numbers only",
                            "Programming error");
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:net.ceos.project.poi.annotated.bean.ObjectMaskBuilder.java

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

    Date date = new Date();
    // Date
    toValidate.setDateAttribute1(date);
    toValidate.setDateAttribute2(date);
    toValidate.setDateAttribute3(date);
    toValidate.setDateAttribute4(date);
    toValidate.setDateAttribute5(date);
    toValidate.setDateAttribute6(date);
    toValidate.setDateAttribute7(date);
    // Double
    toValidate.setDoubleAttribute1(1625.487 * multiplier);
    toValidate.setDoubleAttribute2(1625.487 * multiplier);
    toValidate.setDoubleAttribute3(1625.487 * multiplier);
    toValidate.setDoubleAttribute4(1625.487 * multiplier);
    toValidate.setDoubleAttribute5(1625.487 * multiplier);
    // BigDecimal
    toValidate.setBigDecimalAttribute1(BigDecimal.valueOf(111.366 * multiplier));
    toValidate.setBigDecimalAttribute2(BigDecimal.valueOf(111.366 * multiplier));
    toValidate.setBigDecimalAttribute3(BigDecimal.valueOf(111.366 * multiplier));
    toValidate.setBigDecimalAttribute4(BigDecimal.valueOf(111.366 * multiplier));
    toValidate.setBigDecimalAttribute5(BigDecimal.valueOf(111.366 * multiplier));
    toValidate.setBigDecimalAttribute6(BigDecimal.valueOf(111.366 * multiplier));
    // Float
    toValidate.setFloatAttribute1(46.445f * multiplier);
    toValidate.setFloatAttribute2(46.445f * multiplier);
    toValidate.setFloatAttribute3(46.445f * multiplier);
    toValidate.setFloatAttribute4(46.445f * multiplier);
    toValidate.setFloatAttribute5(46.445f * multiplier);
    // Boolean
    toValidate.setBooleanAttribute1(true);
    toValidate.setBooleanAttribute2(true);
    toValidate.setBooleanAttribute3(false);
    toValidate.setBooleanAttribute4(false);
    // Integer
    toValidate.setIntegerAttribute1(23);
    toValidate.setIntAttribute2(10);
    // Short
    toValidate.setShortAttribute1((short) 5);
    toValidate.setShortAttribute2((short) 44);

    return toValidate;
}

From source file:com.salatigacode.dao.ProductControllerTests.java

@Test
public void testSave() {
    Product p = new Product();
    p.setCode("PT-001");
    p.setName("Product Test 001");
    p.setPrice(BigDecimal.valueOf(102000.02));

    Product responseObj = restTemplate.postForObject(BASE_URL, p, Product.class);
    Assert.assertNotNull(responseObj.getId());

    //nama tidak diisi
    Product px = new Product();
    p.setCode("PT-002");
    Product responseObjx = restTemplate.postForObject(BASE_URL, px, Product.class);
    Assert.assertNull(responseObjx.getId());

}

From source file:com.ancientprogramming.fixedformat4j.format.impl.AbstractDecimalFormatter.java

public String asString(T obj, FormatInstructions instructions) {
    BigDecimal roundedValue = null;
    int decimals = instructions.getFixedFormatDecimalData().getDecimals();
    if (obj != null) {
        BigDecimal value = obj instanceof BigDecimal ? (BigDecimal) obj : BigDecimal.valueOf(obj.doubleValue());

        RoundingMode roundingMode = instructions.getFixedFormatDecimalData().getRoundingMode();

        roundedValue = value.setScale(decimals, roundingMode);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Value before rounding = '" + value + "', value after rounding = '" + roundedValue
                    + "', decimals = " + decimals + ", rounding mode = " + roundingMode);
        }//from   w  w w .j av a 2 s . c  om
    }

    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalSeparatorAlwaysShown(true);
    formatter.setMaximumFractionDigits(decimals);

    char decimalSeparator = formatter.getDecimalFormatSymbols().getDecimalSeparator();
    char groupingSeparator = formatter.getDecimalFormatSymbols().getGroupingSeparator();
    String zeroString = "0" + decimalSeparator + "0";

    String rawString = roundedValue != null ? formatter.format(roundedValue) : zeroString;
    if (LOG.isDebugEnabled()) {
        LOG.debug("rawString: " + rawString + " - G[" + groupingSeparator + "] D[" + decimalSeparator + "]");
    }
    rawString = rawString.replaceAll("\\" + groupingSeparator, "");
    boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter();

    String beforeDelimiter = rawString.substring(0, rawString.indexOf(decimalSeparator));
    String afterDelimiter = rawString.substring(rawString.indexOf(decimalSeparator) + 1, rawString.length());
    if (LOG.isDebugEnabled()) {
        LOG.debug("beforeDelimiter[" + beforeDelimiter + "], afterDelimiter[" + afterDelimiter + "]");
    }

    //trim decimals
    afterDelimiter = StringUtils.substring(afterDelimiter, 0, decimals);
    afterDelimiter = StringUtils.rightPad(afterDelimiter, decimals, '0');

    String delimiter = useDecimalDelimiter ? "" + instructions.getFixedFormatDecimalData().getDecimalDelimiter()
            : "";
    String result = beforeDelimiter + delimiter + afterDelimiter;
    if (LOG.isDebugEnabled()) {
        LOG.debug("result[" + result + "]");
    }
    return result;
}

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

@Test
public void addLegoKitTest() {
    List<LegoKit> legoKitsBefore = em.createQuery("SELECT l FROM LegoKit l", LegoKit.class).getResultList();
    int sizeBefore = legoKitsBefore.size();
    LegoKit legoKit1 = createLegoKit("Star Wars Death Star", BigDecimal.valueOf(42), 12,
            new HashSet<Category>(), new ArrayList<LegoPiece>(), new ArrayList<LegoSet>());
    try {/*from  w w w .ja  v  a  2  s .  c o m*/
        legoKitDao.addLegoKit(legoKit1);
    } catch (Exception e) {
        fail("Exception thrown adding lego kit: " + e.getMessage());
    }
    List<LegoKit> legoKitsAfter = em.createQuery("SELECT l FROM LegoKit l", LegoKit.class).getResultList();
    int sizeAfter = legoKitsAfter.size();
    assertEquals(sizeBefore + 1, sizeAfter);
}

From source file:org.impotch.calcul.impot.cantonal.ge.pp.Baremes2015Test.java

@Test
public void borneBaremeRevenu() {
    Bareme bareme = fournisseur.getBaremeRevenu(2015);
    assertThat(bareme.calcul(BigDecimal.valueOf(17663))).isEqualTo("0.00");
    assertThat(bareme.calcul(BigDecimal.valueOf(21281))).isEqualTo("289.45");
    assertThat(bareme.calcul(BigDecimal.valueOf(23409))).isEqualTo("480.95");
    assertThat(bareme.calcul(BigDecimal.valueOf(25537))).isEqualTo("693.75");
    assertThat(bareme.calcul(BigDecimal.valueOf(27665))).isEqualTo("927.85");
    assertThat(bareme.calcul(BigDecimal.valueOf(32985))).isEqualTo("1566.25");
    assertThat(bareme.calcul(BigDecimal.valueOf(37241))).isEqualTo("2119.55");
    assertThat(bareme.calcul(BigDecimal.valueOf(41498))).isEqualTo("2715.55");
    assertThat(bareme.calcul(BigDecimal.valueOf(45754))).isEqualTo("3332.65");
    assertThat(bareme.calcul(BigDecimal.valueOf(73420))).isEqualTo("7482.55");
    assertThat(bareme.calcul(BigDecimal.valueOf(120238))).isEqualTo("14739.35");
    assertThat(bareme.calcul(BigDecimal.valueOf(161736))).isEqualTo("21379.05");
    assertThat(bareme.calcul(BigDecimal.valueOf(183017))).isEqualTo("24890.40");
    assertThat(bareme.calcul(BigDecimal.valueOf(261757))).isEqualTo("38276.20");
    assertThat(bareme.calcul(BigDecimal.valueOf(278782))).isEqualTo("41255.60");
    assertThat(bareme.calcul(BigDecimal.valueOf(392636))).isEqualTo("61749.30");
    assertThat(bareme.calcul(BigDecimal.valueOf(615022))).isEqualTo("102890.70");
}

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

@Test
public void aggregatesTwoMessagesIntoOne() throws Exception {
    mockAggregated.expectedHeaderReceived("invoiceItemTotal", BigDecimal.valueOf(5));
    mockAggregated.expectedMessageCount(1);

    start.sendBodyAndHeaders(null,//  ww w .  j a  va2 s  . c  om
            toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(2)));
    start.sendBodyAndHeaders(null,
            toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(3)));
    assertMockEndpointsSatisfied();
}

From source file:com.trenako.web.controllers.form.CollectionItemFormTests.java

@Test
public void shouldCreateFormsForCollectionItemsCreations() {
    CollectionItemForm form = CollectionItemForm.newForm(rollingStock(), messageSource);

    assertEquals("[(new), (pre-owned)]", form.getConditionsList().toString());
    assertEquals("acme-123456", form.getRsSlug());
    assertEquals("{slug: acme-123456, label: ACME 123456}",
            form.getItem().getRollingStock().toCompleteString());
    assertEquals(rollingStock().getCategory(), form.getItem().getCategory());
    assertEquals(BigDecimal.valueOf(0), form.getPrice());
    assertNull(form.getItem().getNotes());
    assertNull(form.getItem().getCondition());
    assertNull(form.getItem().getAddedAt());
    assertNull(form.getItem().getItemId());
}