List of usage examples for java.math BigDecimal ROUND_DOWN
int ROUND_DOWN
To view the source code for java.math BigDecimal ROUND_DOWN.
Click Source Link
From source file:org.springframework.data.gemfire.examples.OrderExample.java
private void findOrders() { log.debug("looking for orders for customer ID 2"); for (Order order : orderService.findOrdersByCustomerId(2L)) { log.debug("found order ID " + order.getId() + " " + order.getBillingAddress().getStreet() + " " + order.getBillingAddress().getCity() + " " + order.getBillingAddress().getCountry()); for (LineItem lineItem : order.getLineItems()) { log.debug("product ID:" + lineItem.getProductId() + " quantity:" + lineItem.getAmount() + " unit price:" + lineItem.getUnitPrice().setScale(2, BigDecimal.ROUND_DOWN) + " total price:" + lineItem.getTotal().setScale(2, BigDecimal.ROUND_DOWN)); }// w ww . java2 s. c om } }
From source file:password.pwm.util.localdb.LocalDBUtility.java
private void importLocalDB(final InputStream inputStream, final Appendable out, final long totalBytes) throws PwmOperationalException, IOException { this.prepareForImport(); importLineCounter = 0;/*from w w w . j av a2 s. c o m*/ if (totalBytes > 0) { writeStringToOut(out, "total bytes in localdb import source: " + totalBytes); } writeStringToOut(out, "beginning localdb import..."); final Instant startTime = Instant.now(); final TransactionSizeCalculator transactionCalculator = new TransactionSizeCalculator( new TransactionSizeCalculator.SettingsBuilder() .setDurationGoal(new TimeDuration(100, TimeUnit.MILLISECONDS)).setMinTransactions(50) .setMaxTransactions(5 * 1000).createSettings()); final Map<LocalDB.DB, Map<String, String>> transactionMap = new HashMap<>(); for (final LocalDB.DB loopDB : LocalDB.DB.values()) { transactionMap.put(loopDB, new TreeMap<>()); } final CountingInputStream countingInputStream = new CountingInputStream(inputStream); final EventRateMeter eventRateMeter = new EventRateMeter(TimeDuration.MINUTE); final Timer statTimer = new Timer(true); statTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { String output = ""; if (totalBytes > 0) { final ProgressInfo progressInfo = new ProgressInfo(startTime, totalBytes, countingInputStream.getByteCount()); output += progressInfo.debugOutput(); } else { output += "recordsImported=" + importLineCounter; } output += ", avgTransactionSize=" + transactionCalculator.getTransactionSize() + ", recordsPerMinute=" + eventRateMeter.readEventRate().setScale(2, BigDecimal.ROUND_DOWN); writeStringToOut(out, output); } }, 30 * 1000, 30 * 1000); Reader csvReader = null; try { csvReader = new InputStreamReader(new GZIPInputStream(countingInputStream, GZIP_BUFFER_SIZE), PwmConstants.DEFAULT_CHARSET); for (final CSVRecord record : PwmConstants.DEFAULT_CSV_FORMAT.parse(csvReader)) { importLineCounter++; eventRateMeter.markEvents(1); final String dbName_recordStr = record.get(0); final LocalDB.DB db = JavaHelper.readEnumFromString(LocalDB.DB.class, null, dbName_recordStr); final String key = record.get(1); final String value = record.get(2); if (db == null) { writeStringToOut(out, "ignoring localdb import record #" + importLineCounter + ", invalid DB name '" + dbName_recordStr + "'"); } else { transactionMap.get(db).put(key, value); int cachedTransactions = 0; for (final LocalDB.DB loopDB : LocalDB.DB.values()) { cachedTransactions += transactionMap.get(loopDB).size(); } if (cachedTransactions >= transactionCalculator.getTransactionSize()) { final long startTxnTime = System.currentTimeMillis(); for (final LocalDB.DB loopDB : LocalDB.DB.values()) { localDB.putAll(loopDB, transactionMap.get(loopDB)); transactionMap.get(loopDB).clear(); } transactionCalculator.recordLastTransactionDuration(TimeDuration.fromCurrent(startTxnTime)); } } } } finally { LOGGER.trace("import process completed"); statTimer.cancel(); IOUtils.closeQuietly(csvReader); IOUtils.closeQuietly(countingInputStream); } for (final LocalDB.DB loopDB : LocalDB.DB.values()) { localDB.putAll(loopDB, transactionMap.get(loopDB)); transactionMap.get(loopDB).clear(); } this.markImportComplete(); writeStringToOut(out, "restore complete, restored " + importLineCounter + " records in " + TimeDuration.fromCurrent(startTime).asLongString()); statTimer.cancel(); }
From source file:sg.ncl.MainController.java
private TeamQuota extractTeamQuotaInfo(String responseBody) { JSONObject object = new JSONObject(responseBody); TeamQuota teamQuota = new TeamQuota(); Double charges = Double.parseDouble(accountingProperties.getCharges()); // amountUsed from SIO will never be null => not checking for null value String usage = object.getString(KEY_USAGE); // getting usage in String BigDecimal amountUsed = new BigDecimal(usage); // using BigDecimal to handle currency amountUsed = amountUsed.multiply(new BigDecimal(charges)); // usage X charges //quota passed from SIO can be null , so we have to check for null value if (object.has(QUOTA)) { Object budgetObject = object.optString(QUOTA, null); if (budgetObject == null) { teamQuota.setBudget(""); // there is placeholder here teamQuota.setResourcesLeft("Unlimited"); // not placeholder so can pass string over } else {//from w ww .j a va2 s . co m Double budgetInDouble = object.getDouble(QUOTA); // retrieve budget from SIO in Double BigDecimal budgetInBD = BigDecimal.valueOf(budgetInDouble); // handling currency using BigDecimal // calculate resoucesLeft BigDecimal resourceLeftInBD = budgetInBD.subtract(amountUsed); resourceLeftInBD = resourceLeftInBD.divide(new BigDecimal(charges), 0, BigDecimal.ROUND_DOWN); budgetInBD = budgetInBD.setScale(2, BigDecimal.ROUND_HALF_UP); // set budget teamQuota.setBudget(budgetInBD.toString()); //set resroucesLeft if (resourceLeftInBD.compareTo(BigDecimal.valueOf(0)) < 0) teamQuota.setResourcesLeft("0"); else teamQuota.setResourcesLeft(resourceLeftInBD.toString()); } } //set teamId and amountUsed teamQuota.setTeamId(object.getString(TEAM_ID)); amountUsed = amountUsed.setScale(2, BigDecimal.ROUND_HALF_UP); teamQuota.setAmountUsed(amountUsed.toString()); return teamQuota; }