Example usage for java.math BigDecimal divide

List of usage examples for java.math BigDecimal divide

Introduction

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

Prototype

public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 

Source Link

Document

Returns a BigDecimal whose value is (this / divisor) , and whose scale is as specified.

Usage

From source file:org.projectforge.web.wicket.converter.BigDecimalPercentConverter.java

@Override
public BigDecimal convertToObject(String value, final Locale locale) {
    value = StringUtils.trimToEmpty(value);
    if (value.endsWith("%") == true) {
        value = value.substring(0, value.length() - 1).trim();
    }//ww w. ja  v  a 2  s .c o m
    BigDecimal bd = super.convertToObject(value, locale);
    if (bd != null && decimalFormat == true) {
        bd = bd.divide(NumberHelper.HUNDRED, bd.scale() + 2, RoundingMode.HALF_UP);
    }
    return bd;
}

From source file:com.sunchenbin.store.feilong.core.lang.NumberUtil.java

/**
 *  one/two,?,??.// w w w. java2  s. c  om
 * 
 * <p>
 * two 0,one<br>
 * ?one/two,?,??.
 * </p>
 * 
 * <p>
 * <b>?:</b> <span style="color:red">?one.divide(two),?? exception:Non-terminating decimal expansion; no exact representable decimal
 * result</span><br>
 * scaleroundingMode,?????.
 * </p>
 *
 * @param one
 *            
 * @param two
 *            ,?BigDecimal??
 * @param scale
 *            ? BigDecimal ,??
 * @param roundingMode
 *            ? {@link RoundingMode}
 * @return two 0,one<br>
 *         ?one/two,??? {@link RoundingMode},??
 * @see <a href="#RoundingMode">JAVA 8??</a>
 * @since 1.0.7
 */
public static BigDecimal getDivideValue(BigDecimal one, Serializable two, int scale,
        RoundingMode roundingMode) {

    if (Validator.isNullOrEmpty(roundingMode)) {
        throw new NullPointerException("the roundingMode is null or empty!");
    }
    String zero = "0";
    if (!isSpecificNumber(two, zero)) {
        // ?one.divide(two) 
        // ?? exception:Non-terminating decimal expansion; no exact representable decimal result
        // scaleroundingMode,?????.
        BigDecimal divisor = ConvertUtil.toBigDecimal(two);
        return one.divide(divisor, scale, roundingMode);
    }
    return one;
}

From source file:io.seldon.recommendation.VariationTestingClientStrategyTest.java

@Test
public void rangeTest() {
    BigDecimal ratioTotal = new BigDecimal(1.0);
    BigDecimal currentMax = new BigDecimal(0.0);
    BigDecimal r1 = new BigDecimal(0.9);
    BigDecimal r2 = new BigDecimal(0.1);
    NumberRange range1 = new NumberRange(currentMax,
            currentMax.add(r1.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
    currentMax = currentMax.add(r1);// w  ww  .  ja  v  a 2s  . c o  m
    NumberRange range2 = new NumberRange(currentMax.add(new BigDecimal(0.0001)),
            currentMax.add(r2.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
    BigDecimal t = new BigDecimal(0.900001);
    Assert.assertTrue(range1.containsNumber(t));
    Assert.assertFalse(range2.containsNumber(t));
    BigDecimal t2 = new BigDecimal(0.901);
    Assert.assertFalse(range1.containsNumber(t2));
    Assert.assertTrue(range2.containsNumber(t2));

}

From source file:libra.common.kmermatch.KmerJoiner.java

public float getProgress() {
    if (this.progressKey == null) {
        if (this.eof) {
            return 1.0f;
        } else {//from   w w  w . java  2s  .c  o  m
            return 0.0f;
        }
    } else {
        BigInteger seq = SequenceHelper.convertToBigInteger(this.progressKey.getSequence());
        BigInteger prog = seq.subtract(this.beginSequence);
        int comp = this.partitionSize.compareTo(prog);
        if (comp <= 0) {
            return 1.0f;
        } else {
            BigDecimal progDecimal = new BigDecimal(prog);
            BigDecimal rate = progDecimal.divide(new BigDecimal(this.partitionSize), 3,
                    BigDecimal.ROUND_HALF_UP);

            float f = rate.floatValue();
            return Math.min(1.0f, f);
        }
    }
}

From source file:org.libreplan.business.planner.entities.PlanningData.java

/**
 * Prevents division by zero//from  ww  w .j a  v  a  2 s.  c o  m
 *
 * @param numerator
 * @param denominator
 * @return
 */
private BigDecimal divide(BigDecimal numerator, int denominator) {
    if (denominator == 0) {
        return BigDecimal.ZERO;
    }
    return numerator.divide(BigDecimal.valueOf(denominator), 8, BigDecimal.ROUND_HALF_EVEN);
}

From source file:de.csdev.ebus.command.datatypes.ext.EBusTypeDate.java

@Override
public byte[] encodeInt(Object data) throws EBusTypeException {

    IEBusType<BigDecimal> bcdType = types.getType(EBusTypeBCD.TYPE_BCD);
    IEBusType<BigDecimal> wordType = types.getType(EBusTypeWord.TYPE_WORD);
    IEBusType<BigDecimal> charType = types.getType(EBusTypeChar.TYPE_CHAR);

    Calendar calendar = null;//from   www  .  j  a v a  2s . c  o  m
    byte[] result = new byte[this.getTypeLength()];

    if (data instanceof EBusDateTime) {
        calendar = ((EBusDateTime) data).getCalendar();

    } else if (data instanceof Calendar) {
        calendar = (Calendar) data;
    }

    // set date to midnight
    calendar = (Calendar) calendar.clone();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    if (calendar != null) {
        if (StringUtils.equals(variant, DEFAULT)) {

            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            dayOfWeek = dayOfWeek == 1 ? 7 : dayOfWeek - 1;

            result = new byte[] { bcdType.encode(calendar.get(Calendar.DAY_OF_MONTH))[0],
                    bcdType.encode(calendar.get(Calendar.MONTH) + 1)[0], bcdType.encode(dayOfWeek)[0],
                    bcdType.encode(calendar.get(Calendar.YEAR) % 100)[0] };

        } else if (StringUtils.equals(variant, SHORT)) {

            result = new byte[] { bcdType.encode(calendar.get(Calendar.DAY_OF_MONTH))[0],
                    bcdType.encode(calendar.get(Calendar.MONTH) + 1)[0],
                    bcdType.encode(calendar.get(Calendar.YEAR) % 100)[0] };

        } else if (StringUtils.equals(variant, HEX)) {

            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            dayOfWeek = dayOfWeek == 1 ? 7 : dayOfWeek - 1;

            result = new byte[] { charType.encode(calendar.get(Calendar.DAY_OF_MONTH))[0],
                    charType.encode(calendar.get(Calendar.MONTH) + 1)[0], charType.encode(dayOfWeek)[0],
                    charType.encode(calendar.get(Calendar.YEAR) % 100)[0] };

        } else if (StringUtils.equals(variant, HEX_SHORT)) {

            result = new byte[] { charType.encode(calendar.get(Calendar.DAY_OF_MONTH))[0],
                    charType.encode(calendar.get(Calendar.MONTH) + 1)[0],
                    charType.encode(calendar.get(Calendar.YEAR) % 100)[0] };

        } else if (StringUtils.equals(variant, DAYS)) {

            long millis = calendar.getTimeInMillis();

            calendar.clear();
            calendar.set(1900, 0, 1, 0, 0);
            long millis1900 = calendar.getTimeInMillis();

            BigDecimal days = new BigDecimal(millis - millis1900);
            days = days.divide(BigDecimal.valueOf(86400000), 0, RoundingMode.HALF_UP);

            result = wordType.encode(days);
        }
    }

    return result;
}

From source file:com.qcadoo.mes.cmmsMachineParts.controller.MachinePartMultiUploadController.java

@ResponseBody
@RequestMapping(value = "/multiUploadFiles", method = RequestMethod.POST)
public void upload(MultipartHttpServletRequest request, HttpServletResponse response) {
    Long part = Long.parseLong(request.getParameter("partId"));
    Entity technology = dataDefinitionService
            .get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_PRODUCT).get(part);
    DataDefinition attachmentDD = dataDefinitionService.get("cmmsMachineParts", "machinePartAttachment");

    Iterator<String> itr = request.getFileNames();
    MultipartFile mpf = null;// ww  w.  ja va 2  s . c o m

    while (itr.hasNext()) {

        mpf = request.getFile(itr.next());

        String path = "";
        try {
            path = fileService.upload(mpf);
        } catch (IOException e) {
            logger.error("Unable to upload attachment.", e);
        }
        if (exts.contains(Files.getFileExtension(path).toUpperCase())) {
            Entity atchment = attachmentDD.create();
            atchment.setField(TechnologyAttachmentFields.ATTACHMENT, path);
            atchment.setField(TechnologyAttachmentFields.NAME, mpf.getOriginalFilename());
            atchment.setField("product", technology);
            atchment.setField(TechnologyAttachmentFields.EXT, Files.getFileExtension(path));
            BigDecimal fileSize = new BigDecimal(mpf.getSize(), numberService.getMathContext());
            BigDecimal divider = new BigDecimal(1024, numberService.getMathContext());
            BigDecimal size = fileSize.divide(divider, L_SCALE, BigDecimal.ROUND_HALF_UP);
            atchment.setField(TechnologyAttachmentFields.SIZE, size);
            atchment = attachmentDD.save(atchment);
            atchment.isValid();
        }
    }
}

From source file:com.qcadoo.mes.basic.controllers.WorkstationMultiUploadController.java

@ResponseBody
@RequestMapping(value = "/multiUploadFiles", method = RequestMethod.POST)
public void upload(MultipartHttpServletRequest request, HttpServletResponse response) {
    Long workstationId = Long.parseLong(request.getParameter("techId"));
    Entity workstation = dataDefinitionService
            .get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_WORKSTATION).get(workstationId);
    DataDefinition attachmentDD = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER,
            BasicConstants.MODEL_WORKSTATION_ATTACHMENT);

    Iterator<String> itr = request.getFileNames();
    MultipartFile mpf = null;/*from   w w  w.  j a v  a  2  s . c  o m*/

    while (itr.hasNext()) {

        mpf = request.getFile(itr.next());

        String path = "";
        try {
            path = fileService.upload(mpf);
        } catch (IOException e) {
            logger.error("Unable to upload attachment.", e);
        }
        if (exts.contains(Files.getFileExtension(path).toUpperCase())) {
            Entity atchment = attachmentDD.create();
            atchment.setField(WorkstationAttachmentFields.ATTACHMENT, path);
            atchment.setField(WorkstationAttachmentFields.NAME, mpf.getOriginalFilename());
            atchment.setField(WorkstationAttachmentFields.WORKSTATION, workstation);
            atchment.setField(WorkstationAttachmentFields.EXT, Files.getFileExtension(path));
            BigDecimal fileSize = new BigDecimal(mpf.getSize(), numberService.getMathContext());
            BigDecimal divider = new BigDecimal(1024, numberService.getMathContext());
            BigDecimal size = fileSize.divide(divider, L_SCALE, BigDecimal.ROUND_HALF_UP);
            atchment.setField(WorkstationAttachmentFields.SIZE, size);
            attachmentDD.save(atchment);
        }
    }
}

From source file:com.qcadoo.mes.basic.controllers.SubassemblyMultiUploadController.java

@ResponseBody
@RequestMapping(value = "/multiUploadFilesForSubassembly", method = RequestMethod.POST)
public void upload(MultipartHttpServletRequest request, HttpServletResponse response) {
    Long subassemblyId = Long.parseLong(request.getParameter("techId"));
    Entity subassembly = dataDefinitionService
            .get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_SUBASSEMBLY).get(subassemblyId);
    DataDefinition attachmentDD = dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER,
            BasicConstants.MODEL_SUBASSEMBLY_ATTACHMENT);

    Iterator<String> itr = request.getFileNames();
    MultipartFile mpf = null;/* ww  w  .j a v  a  2 s.  c  o m*/

    while (itr.hasNext()) {

        mpf = request.getFile(itr.next());

        String path = "";
        try {
            path = fileService.upload(mpf);
        } catch (IOException e) {
            logger.error("Unable to upload attachment.", e);
        }
        if (exts.contains(Files.getFileExtension(path).toUpperCase())) {
            Entity atchment = attachmentDD.create();
            atchment.setField(SubassemblyAttachmentFields.ATTACHMENT, path);
            atchment.setField(SubassemblyAttachmentFields.NAME, mpf.getOriginalFilename());
            atchment.setField(SubassemblyAttachmentFields.SUBASSEMBLY, subassembly);
            atchment.setField(SubassemblyAttachmentFields.EXT, Files.getFileExtension(path));
            BigDecimal fileSize = new BigDecimal(mpf.getSize(), numberService.getMathContext());
            BigDecimal divider = new BigDecimal(1024, numberService.getMathContext());
            BigDecimal size = fileSize.divide(divider, L_SCALE, BigDecimal.ROUND_HALF_UP);
            atchment.setField(SubassemblyAttachmentFields.SIZE, size);
            attachmentDD.save(atchment);
        }
    }
}

From source file:com.qcadoo.mes.cmmsMachineParts.controller.MaintenanceEventMultiUploadController.java

@ResponseBody
@RequestMapping(value = "/multiUploadFilesForEvent", method = RequestMethod.POST)
public void upload(MultipartHttpServletRequest request, HttpServletResponse response) {
    Long eventId = Long.parseLong(request.getParameter("eventId"));
    Entity event = dataDefinitionService
            .get(CmmsMachinePartsConstants.PLUGIN_IDENTIFIER, CmmsMachinePartsConstants.MODEL_MAINTENANCE_EVENT)
            .get(eventId);/*ww  w. ja  v  a2s  .  co  m*/
    DataDefinition attachmentDD = dataDefinitionService.get("cmmsMachineParts", "eventAttachment");

    Iterator<String> itr = request.getFileNames();
    MultipartFile mpf = null;

    while (itr.hasNext()) {

        mpf = request.getFile(itr.next());

        String path = "";
        try {
            path = fileService.upload(mpf);
        } catch (IOException e) {
            logger.error("Unable to upload attachment.", e);
        }
        if (exts.contains(Files.getFileExtension(path).toUpperCase())) {
            Entity atchment = attachmentDD.create();
            atchment.setField(TechnologyAttachmentFields.ATTACHMENT, path);
            atchment.setField(TechnologyAttachmentFields.NAME, mpf.getOriginalFilename());
            atchment.setField("maintenanceEvent", event);
            atchment.setField(TechnologyAttachmentFields.EXT, Files.getFileExtension(path));
            BigDecimal fileSize = new BigDecimal(mpf.getSize(), numberService.getMathContext());
            BigDecimal divider = new BigDecimal(1024, numberService.getMathContext());
            BigDecimal size = fileSize.divide(divider, L_SCALE, BigDecimal.ROUND_HALF_UP);
            atchment.setField(TechnologyAttachmentFields.SIZE, size);
            atchment = attachmentDD.save(atchment);
            atchment.isValid();
        }
    }
}