public static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); BigDecimal bd = new BigDecimal(value); bd = bd.setScale(places, RoundingMode.HALF_UP); return bd.doubleValue(); }
/** * Creates JD from <code>BigDecimal</code>. */ public JulianDateStamp(BigDecimal bd) { double d = bd.doubleValue(); integer = (int) d; bd = bd.subtract(new BigDecimal(integer)); fraction = bd.doubleValue(); }
Double toBeTruncated = new Double("3.5789055"); Double truncatedDouble = BigDecimal.valueOf(toBeTruncated) .setScale(3, RoundingMode.HALF_UP) .doubleValue();
@Test public void shouldRespectWriteExpression() { EntityManager em = getEntityManager(); em.getTransaction().begin(); List resultList = em.createNativeQuery( "select size_in_cm from t_staff_AUD where id =" + id ).getResultList(); Assert.assertEquals( 1, resultList.size() ); Double sizeInCm = null; if ( getDialect() instanceof Oracle8iDialect ) { sizeInCm = ((BigDecimal) resultList.get( 0 )).doubleValue(); } else { sizeInCm = (Double) resultList.get( 0 ); } em.getTransaction().commit(); Assert.assertEquals( HEIGHT_CENTIMETERS, sizeInCm.doubleValue(), 0.00000001 ); }
@Override public Object execute(List<ExpressionData<?>> dataList, Context context,Cell currentCell) { BigDecimal data=buildBigDecimal(dataList); int pos=0; if(dataList.size()==2){ ExpressionData<?> exprData=dataList.get(1); if(exprData instanceof ObjectExpressionData){ ObjectExpressionData objData=(ObjectExpressionData)exprData; Object obj=objData.getData(); if(obj==null){ throw new ReportComputeException("Ceil Function second parameter can not be null."); } pos=Utils.toBigDecimal(obj).intValue(); } } data.setScale(pos, BigDecimal.ROUND_CEILING); return Math.ceil(data.doubleValue()); }
subTotal = new BigDecimal(0); subTotal.setScale(2, RoundingMode.HALF_UP); amnt = new BigDecimal(0); amnt.setScale(2, RoundingMode.HALF_UP); if(shippingSummary!=null && shippingSummary.getShipping()!=null && shippingSummary.getShipping().doubleValue()>0) { amnt = amnt.add(shippingSummary.getShipping()); if(shippingSummary.getHandling()!=null && shippingSummary.getHandling().doubleValue()>0) { amnt = amnt.add(shippingSummary.getHandling()); double taxRateDouble = taxRate.getTaxRate().doubleValue();//5% ... 8% ... if(totalTaxedItemValue.doubleValue()>0) { beforeTaxeAmount = totalTaxedItemValue; double value = (beforeTaxeAmount.doubleValue() * taxRateDouble)/100; double roundedValue = new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue(); taxedItemValue = new BigDecimal(roundedValue).setScale(2, RoundingMode.HALF_UP); totalTaxedItemValue = beforeTaxeAmount.add(taxedItemValue); taxItem.setLabel(taxRate.getDescriptions().get(0).getName()); taxItem.setTaxRate(taxRate); taxItems.add(taxItem);
@Test @TestForIssue(jiraKey = "HHH-12280") public void testFormulaWithAlias() throws Exception { doInHibernate( this::sessionFactory, session -> { Customer company1 = new Customer(); company1.setBalance(new BigDecimal(100)); company1.setVip(true); session.persist(company1); Customer company2 = new Customer(); company2.setBalance(new BigDecimal(1000)); company2.setVip(false); session.persist(company2); } ); doInHibernate( this::sessionFactory, session -> { List<Customer> customers = session.createQuery( "select c " + "from Customer c ", Customer.class) .getResultList(); assertEquals(2, customers.size()); assertEquals(1d, customers.get(0).getPercentage().doubleValue(), 0); assertEquals(1d, customers.get(1).getPercentage().doubleValue(), 0); } ); }
/** * 计算比率。计算结果四舍五入。 * * @param numerator 分子 * @param denominator 分母 * @param scale 保留小数点后位数 * @return 比率 */ public static double divide(long numerator, long denominator, int scale) { BigDecimal numeratorBd = new BigDecimal(numerator); BigDecimal denominatorBd = new BigDecimal(denominator); return numeratorBd.divide(denominatorBd, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); }
/** * Using BigDecimal to avoid issues with overflow and underflow. * * @param value - value * @param divisor - divisor. * @return -- returns a double that represents this value */ private static double divide(double value, double divisor) { BigDecimal val = new BigDecimal(value); BigDecimal bDivisor = new BigDecimal(divisor); return val.divide(bDivisor).setScale(PRECISION, RoundingMode.HALF_UP) .doubleValue(); }
BigDecimal bigSeconds = BigDecimal.valueOf(Math.abs(durationInMillis)).divide(new BigDecimal(1000)); double seconds = bigSeconds.doubleValue() - dMinutes * 60;
private static Number unwrapNumber(final Number n) { Number unwrapped; if (!isPrimitiveNumber(n)) { BigDecimal bigDecimal = new BigDecimal(n.toString()); if (bigDecimal.scale() <= 0) { if (bigDecimal.compareTo(new BigDecimal(Integer.MAX_VALUE)) <= 0) { unwrapped = bigDecimal.intValue(); } else if (bigDecimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0){ unwrapped = bigDecimal.longValue(); } else { unwrapped = bigDecimal; } } else { final double doubleValue = bigDecimal.doubleValue(); if (BigDecimal.valueOf(doubleValue).compareTo(bigDecimal) != 0) { unwrapped = bigDecimal; } else { unwrapped = doubleValue; } } } else { unwrapped = n; } return unwrapped; }
protected double trunc(double input, int scale) { return trunc(BigDecimal.valueOf(input), scale).doubleValue(); }
/** * get the value which is converted to specified unit. * * @param unit size unit * @return the converted value */ public double get(Unit unit) { if (value == 0) { return value; } int diff = this.unit.getOrderOfSize() - unit.getOrderOfSize(); if (diff == 0) { return value; } BigDecimal rval = BigDecimal.valueOf(value); for (int i = 0; i != Math.abs(diff); ++i) { rval = diff > 0 ? rval.multiply(SCALE_BASE) : rval.divide(SCALE_BASE); } return rval.doubleValue(); }
/** * Convert an absolute error to a precision. * * @param x The value of the variable * @param xerr The absolute error in the variable * @return The number of valid digits in x. * The value is rounded down, and on the pessimistic side for that reason. */ static public int err2prec(BigDecimal x, BigDecimal xerr) { return err2prec(xerr.divide(x, MathContext.DECIMAL64).doubleValue()); }
/** * Returns a random double * * @param maxNumberOfDecimals maximum number of places * @param min minimum value * @param max maximum value */ public double randomDouble(int maxNumberOfDecimals, long min, long max) { return decimalBetween(min,max) .setScale(maxNumberOfDecimals, BigDecimal.ROUND_HALF_DOWN) .doubleValue(); }
return NullableValue.of(nativeType, (long) floatToRawIntBits(row.getFloat(i))); case DECIMAL: return NullableValue.of(nativeType, row.getDecimal(i).doubleValue()); case UUID: case TIMEUUID: case SET: checkTypeArguments(cassandraType, 1, typeArguments); return NullableValue.of(nativeType, utf8Slice(buildSetValue(row, i, typeArguments.get(0)))); case LIST: checkTypeArguments(cassandraType, 1, typeArguments); return NullableValue.of(nativeType, utf8Slice(buildListValue(row, i, typeArguments.get(0)))); case MAP: checkTypeArguments(cassandraType, 2, typeArguments); return NullableValue.of(nativeType, utf8Slice(buildMapValue(row, i, typeArguments.get(0), typeArguments.get(1)))); default: throw new IllegalStateException("Handling of type " + cassandraType
@Override public Object execute(List<ExpressionData<?>> dataList, Context context,Cell currentCell) { BigDecimal data=buildBigDecimal(dataList); int pos=0; if(dataList.size()==2){ ExpressionData<?> exprData=dataList.get(1); if(exprData instanceof ObjectExpressionData){ ObjectExpressionData objData=(ObjectExpressionData)exprData; Object obj=objData.getData(); if(obj==null){ throw new ReportComputeException("Floor Function second parameter can not be null."); } pos=Utils.toBigDecimal(obj).intValue(); } } data.setScale(pos, BigDecimal.ROUND_FLOOR); return Math.floor(data.doubleValue()); }
public static double round(double unrounded, int precision, int roundingMode) { BigDecimal bd = new BigDecimal(unrounded); BigDecimal rounded = bd.setScale(precision, roundingMode); return rounded.doubleValue(); }
/** * 计算比率。计算结果四舍五入。 * * @param numerator 分子 * @param denominator 分母 * @param scale 保留小数点后位数 * @return 比率 */ public static double divide(long numerator, long denominator, int scale) { BigDecimal numeratorBd = new BigDecimal(numerator); BigDecimal denominatorBd = new BigDecimal(denominator); return numeratorBd.divide(denominatorBd, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); }
BigDecimal _0_1 = new BigDecimal(0.1); BigDecimal x = _0_1; for(int i = 1; i <= 10; i ++) { System.out.println(i+" x 0.1 is "+x+", as double "+x.doubleValue()); x = x.add(_0_1); }