List of usage examples for java.math RoundingMode FLOOR
RoundingMode FLOOR
To view the source code for java.math RoundingMode FLOOR.
Click Source Link
From source file:com.reizes.shiva.utils.CommonUtil.java
/** * ? ( ?)/* w w w .j a va 2 s. c om*/ * @param input * @return */ public static String decimalPointTwo(Double input) { if (input == null || input == 0) { return "0.0"; } DecimalFormat df = new DecimalFormat("#.##"); df.setRoundingMode(RoundingMode.FLOOR); return df.format(input); }
From source file:com.ufnet.ws.service.UserService.java
/** * cardDelUser/* w ww .j a v a 2 s . com*/ * @param request * @return */ public int cardDelUser(CardDelUserRequest request) { // Request && Validation String userId = request.getUserId(); if (StringUtils.isBlank(userId)) { log.error("userid[" + userId + "] is blank."); return -1; } // Process int returnCode = -1; try { // ? UserInfo userInfo = userInfoRepository.select(userId); if (userInfo != null) { returnCode = new BigDecimal(userInfo.getAvailableAmount(), new MathContext(0, RoundingMode.FLOOR)) .toBigInteger().intValue(); // userInfoRepository.delete(userId); userIpMacRepository.delete(userInfo.getAccountId()); } else { log.error("userid[" + userId + "] not exists."); return -1; } } catch (Exception e) { log.error(ExceptionUtils.getStackTrace(e)); } return returnCode; }
From source file:com.vsthost.rnd.commons.math.ext.linear.DMatrixUtils.java
/** * Returns the DOWN rounded value of the given value for the given steps. * * @param value The original value to be rounded. * @param steps The steps.//from www . j a va 2 s . c om * @return The DOWN rounded value of the given value for the given steps. */ public static BigDecimal roundDownTo(double value, double steps) { final BigDecimal bValue = BigDecimal.valueOf(value); final BigDecimal bSteps = BigDecimal.valueOf(steps); if (Objects.equals(bSteps, BigDecimal.ZERO)) { return bValue; } else { return bValue.divide(bSteps, 0, RoundingMode.FLOOR).multiply(bSteps); } }
From source file:com.meltmedia.dropwizard.etcd.cluster.ClusterAssignmentIT.java
@Test public void shouldBlueGreenDeployWithNewServices() throws InterruptedException { int processCount = 20; for (int i = 0; i < processCount; i++) { dao.put("id" + i, processNode(null, "name" + i)); }// ww w .ja v a 2 s. com int halfFloorCount = IntMath.divide(processCount, 2, RoundingMode.FLOOR); int halfCeilCount = IntMath.divide(processCount, 2, RoundingMode.CEILING); ClusterNode node0 = new ClusterNode().withId("node0").withStartedAt(new DateTime()); MetricRegistry registry = new MetricRegistry(); ClusterService currentClusterService = ClusterService.builder().withEtcdFactory(factoryRule.getFactory()) .withExecutor(executor).withNodesDirectory(nodeDir).withThisNode(node0).withMetricRegistry(registry) .build(); currentClusterService.start(); ProcessService<ObjectNode> currentProcessService = currentClusterService.newProcessService(processDir, ClusterAssignmentIT::toLifecycle, new TypeReference<ObjectNode>() { }); currentProcessService.start(); assertState("only green running", s -> s.assignments("node0") == processCount); for (int i = 1; i < 4; i++) { ClusterNode nextNode = new ClusterNode().withId("node" + i).withStartedAt(new DateTime()); MetricRegistry nextRegistry = new MetricRegistry(); ClusterService nextClusterService = ClusterService.builder().withEtcdFactory(factoryRule.getFactory()) .withExecutor(executor).withNodesDirectory(nodeDir).withThisNode(nextNode) .withMetricRegistry(nextRegistry).build(); nextClusterService.start(); ProcessService<ObjectNode> nextProcessService = nextClusterService.newProcessService(processDir, ClusterAssignmentIT::toLifecycle, new TypeReference<ObjectNode>() { }); nextProcessService.start(); assertState( "blue and green running", s -> s.maxAssignments() == halfCeilCount && s.minAssignments() == halfFloorCount && s.unassigned() == 0, s -> s.unassigned() > 1); final Runnable stopProcess = currentProcessService::stop; final Runnable stopClusterService = currentClusterService::stop; executor.schedule(() -> { stopProcess.run(); stopClusterService.run(); }, 1, TimeUnit.MILLISECONDS); assertState("blue now green", s -> s.assignments(nextProcessService.getId()) == processCount, s -> s.unassigned() > 1); currentProcessService = nextProcessService; currentClusterService = nextClusterService; } currentProcessService.stop(); currentClusterService.stop(); assertState("all stopped", s -> s.unassigned() == processCount); }
From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java
/** * Return the number of decimals for the given number, which can be <code>null</code>, in which case this method returns zero. * /*from w w w .j a va2 s . c o m*/ * @return the number of decimal in the number or zero if there are none or the amount is <code>null</code> * @since 7.8 */ public static int decimalsInAmount(final BigDecimal amount) { if (amount != null) { BigDecimal d = new BigDecimal(amount.toString()); BigDecimal result = d.subtract(d.setScale(0, RoundingMode.FLOOR)).movePointRight(d.scale()); if (result.intValue() != 0) { return result.toString().length(); } } return 0; }
From source file:jp.furplag.util.commons.NumberUtils.java
/** * {@link java.lang.Math#floor(double)}. * * @param o the object, number or string. * @param scale scale of fraction./*from ww w .j a v a 2s .c om*/ * @param type return type. * @return {@code floor(o)}. Return null if o could not convertible to number. */ public static <T extends Number> T floor(final Object o, final Number scale, final Class<T> type) { return setScale(o, scale, RoundingMode.FLOOR, type); }
From source file:eu.europa.ec.fisheries.uvms.rules.service.business.AbstractFact.java
public int numberOfDecimals(BigDecimal value) { if (value == null) { return -1; }/*from ww w. j a v a 2 s .c o m*/ int i = value.subtract(value.setScale(0, RoundingMode.FLOOR)).movePointRight(value.scale()).intValue(); return Integer.toString(i).length(); }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#divide(java.lang.Object, java.lang.Number, java.lang.Number, java.math.RoundingMode, java.lang.Class)}. *///from w w w .j av a2 s . com @SuppressWarnings("unchecked") @Test public void testDivideTNumberNumberRoundingModeClassOfT() { assertEquals("null", null, divide(null, null, null, null, null)); assertEquals("null", null, divide(10, 5, 0, null, null)); assertEquals("null", null, divide(10, 5, null, null, null)); assertEquals("null", null, divide(10, 3, 2, RoundingMode.DOWN, null)); assertEquals("null", (Object) 3.33f, divide(10, 3, 2, RoundingMode.DOWN, float.class)); try { for (Class<?> type : PRIMITIVES) { Object expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class) .invoke(null, "0"); assertEquals("fallback: " + type.getSimpleName(), expected, divide(null, null, null, null, (Class<? extends Number>) type)); } for (Class<?> type : NUMBERS) { Object expected = valueOf(3.33, (Class<? extends Number>) type); assertEquals("10 / 3: " + type.getSimpleName(), expected, divide(10, 3, 2, RoundingMode.DOWN, (Class<? extends Number>) type)); assertEquals("10 / 3: " + type.getSimpleName(), expected, divide(10, 3, 2, RoundingMode.HALF_EVEN, (Class<? extends Number>) type)); assertEquals("10 / 3: " + type.getSimpleName(), expected, divide(10, 3, 2, RoundingMode.HALF_UP, (Class<? extends Number>) type)); assertEquals("10 / 3: " + type.getSimpleName(), expected, divide(10, 3, 2, RoundingMode.FLOOR, (Class<? extends Number>) type)); expected = valueOf(3.34, (Class<? extends Number>) type); assertEquals("10 / 3: " + type.getSimpleName(), expected, divide(10, 3, 2, RoundingMode.UP, (Class<? extends Number>) type)); assertEquals("10 / 3: " + type.getSimpleName(), expected, divide(10, 3, 2, RoundingMode.CEILING, (Class<? extends Number>) type)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#floor(java.lang.Object)}. *//*from w w w . ja va2 s. c o m*/ @Test public void testFloorObject() { assertEquals("null", null, floor((Object) null)); assertEquals("null", null, floor("")); assertEquals("null", null, floor("not a number.")); assertEquals("PI: Float: ", 3f, floor("3.141592653589793f")); assertEquals("PI: Double: ", 3d, floor("3.141592653589793d")); assertEquals("PI: BigDecimal: ", BigDecimal.valueOf(Math.PI).setScale(0, RoundingMode.FLOOR), floor((Object) BigDecimal.valueOf(Math.PI))); }
From source file:controllers.Send.java
public static Result receipt(String idToken) { try {//w w w. jav a 2 s . c o m models.Transaction transaction = ApiHelper.getTransaction(idToken); transaction.senderAmount.setScale(6, RoundingMode.FLOOR); transaction.beneficiaryAmount.setScale(6, RoundingMode.FLOOR); return ok(receipt.render(null, transaction)); } catch (ApiException e) { return ok(receipt.render(e.getMessage(), null)); } }