java.math.BigDecimal.doubleValue()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(244)

本文整理了Java中java.math.BigDecimal.doubleValue()方法的一些代码示例,展示了BigDecimal.doubleValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigDecimal.doubleValue()方法的具体详情如下:
包路径:java.math.BigDecimal
类名称:BigDecimal
方法名:doubleValue

BigDecimal.doubleValue介绍

[英]Returns this BigDecimal as a double value. If this is too big to be represented as an float, then Double.POSITIVE_INFINITYor Double.NEGATIVE_INFINITY is returned.

Note, that if the unscaled value has more than 53 significant digits, then this decimal cannot be represented exactly in a double variable. In this case the result is rounded.

For example, if the instance x1 = new BigDecimal("0.1") cannot be represented exactly as a double, and thus x1.equals(new returns false for this case.

Similarly, if the instance new BigDecimal(9007199254740993L) is converted to a double, the result is 9.007199254740992E15.
[中]将此BigDecimal作为双精度值返回。如果该值太大,无法表示为浮点值,则加倍。正无穷大或双精度。返回负无穷大。
请注意,如果未标度值的有效位数超过53位,则该十进制数无法在双变量中精确表示。在这种情况下,结果是四舍五入的。
例如,如果实例x1=new BigDecimal(“0.1”)不能精确地表示为double,则为x1。equals(new)在这种情况下返回false。
类似地,如果实例new BigDecimal(9007199254740993L)转换为double,则结果为9.007199254740992E15。

代码示例

代码示例来源:origin: stackoverflow.com

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();
}

代码示例来源:origin: redisson/redisson

/**
 * 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();
}

代码示例来源:origin: stackoverflow.com

Double toBeTruncated = new Double("3.5789055");

Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
  .setScale(3, RoundingMode.HALF_UP)
  .doubleValue();

代码示例来源:origin: hibernate/hibernate-orm

@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 );
}

代码示例来源:origin: youseries/ureport

@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());
}

代码示例来源:origin: shopizer-ecommerce/shopizer

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);

代码示例来源:origin: hibernate/hibernate-orm

@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);
  } );
}

代码示例来源:origin: alipay/sofa-rpc

/**
 * 计算比率。计算结果四舍五入。
 *
 * @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();
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * 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();
}

代码示例来源:origin: debezium/debezium

BigDecimal bigSeconds = BigDecimal.valueOf(Math.abs(durationInMillis)).divide(new BigDecimal(1000));
double seconds = bigSeconds.doubleValue() - dMinutes * 60;

代码示例来源:origin: json-path/JsonPath

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;
}

代码示例来源:origin: apache/hive

protected double trunc(double input, int scale) {
 return trunc(BigDecimal.valueOf(input), scale).doubleValue();
}

代码示例来源:origin: apache/hbase

/**
 * 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();
}

代码示例来源:origin: deeplearning4j/nd4j

/**
 * 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());
}

代码示例来源:origin: DiUS/java-faker

/**
 * 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();
}

代码示例来源:origin: prestodb/presto

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

代码示例来源:origin: youseries/ureport

@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());
}

代码示例来源:origin: stackoverflow.com

public static double round(double unrounded, int precision, int roundingMode)
{
  BigDecimal bd = new BigDecimal(unrounded);
  BigDecimal rounded = bd.setScale(precision, roundingMode);
  return rounded.doubleValue();
}

代码示例来源:origin: alipay/sofa-rpc

/**
 * 计算比率。计算结果四舍五入。
 *
 * @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();
}

代码示例来源:origin: stackoverflow.com

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);
}

相关文章