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

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

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

BigDecimal.<init>介绍

[英]Constructs a new BigDecimal instance from the 64bit double val. The constructed big decimal is equivalent to the given double. For example, new BigDecimal(0.1) is equal to 0.1000000000000000055511151231257827021181583404541015625. This happens as 0.1 cannot be represented exactly in binary.

To generate a big decimal instance which is equivalent to 0.1 use the BigDecimal(String) constructor.
[中]从64位double val构造新的BigDecimal实例。构造的big decimal等价于给定的double。例如,新的BigDecimal(0.1)等于0.1000000000000005555115123125782702118158340451015625。这是因为0.1不能用二进制精确表示。
要生成相当于0.1的大十进制实例,请使用BigDecimal(String)构造函数。

代码示例

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

public BigDecimal decimalValue(MathContext mc){
  BigDecimal numerator = new BigDecimal(this.numerator);
  BigDecimal denominator = new BigDecimal(this.denominator);

  return numerator.divide(denominator, mc);
}

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

System.out.println(new BigDecimal(1.03).subtract(new BigDecimal(0.41)));
System.out.println(new BigDecimal("1.03").subtract(new BigDecimal("0.41")));

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

new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()

代码示例来源:origin: spring-projects/spring-framework

@Test
public void parseBigDecimalNumber1() {
  String bigDecimalAsString = "0.10";
  Number bigDecimal = NumberUtils.parseNumber(bigDecimalAsString, BigDecimal.class);
  assertEquals(new BigDecimal(bigDecimalAsString), bigDecimal);
}

代码示例来源: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);
}

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

public static void main(String[] args) {
 String doubleVal = "1.745";
 String doubleVal1 = "0.745";
 BigDecimal bdTest = new BigDecimal(  doubleVal);
 BigDecimal bdTest1 = new BigDecimal(  doubleVal1 );
 bdTest = bdTest.setScale(2, BigDecimal.ROUND_HALF_UP);
 bdTest1 = bdTest1.setScale(2, BigDecimal.ROUND_HALF_UP);
 System.out.println("bdTest:"+bdTest); //1.75
 System.out.println("bdTest1:"+bdTest1);//0.75, no problem
}

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

double r = 5.1234;
System.out.println(r); // r is 5.1234

int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(r);

// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
r = bd.doubleValue();

System.out.println(r); // r is 5.12

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

@Test
public void test_getObject_handles_DECIMAL_2() throws SQLException {
 assertThat( testDataRow.getObject( "C_DECIMAL_10.10" ),
       equalTo( (Object) new BigDecimal( "10.10" ) ) );
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void convertBigDecimalToBigInteger() {
  String number = "987459837583750387355346";
  BigDecimal decimal = new BigDecimal(number);
  assertEquals(new BigInteger(number), NumberUtils.convertNumberToTargetClass(decimal, BigInteger.class));
}

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

BigDecimal test = new BigDecimal(0);
System.out.println(test);
test = test.add(new BigDecimal(30));
System.out.println(test);
test = test.add(new BigDecimal(45));
System.out.println(test);

代码示例来源:origin: spring-projects/spring-framework

@Test
public void setNumberPropertiesWithCoercion() {
  NumberTestBean target = new NumberTestBean();
    accessor.setPropertyValue("short2", new Integer(2));
    accessor.setPropertyValue("int2", new Long(8));
    accessor.setPropertyValue("long2", new BigInteger("6"));
    accessor.setPropertyValue("bigInteger", new Integer(3));
    accessor.setPropertyValue("float2", new Double(8.1));
    accessor.setPropertyValue("double2", new BigDecimal(6.1));
    accessor.setPropertyValue("bigDecimal", new Float(4.0));
  assertTrue("Correct long2 value", new Long("6").equals(accessor.getPropertyValue("long2")));
  assertTrue("Correct long2 value", new Long("6").equals(target.getLong2()));
  assertTrue("Correct bigInteger value", new BigInteger("3").equals(accessor.getPropertyValue("bigInteger")));
  assertTrue("Correct bigInteger value", new BigInteger("3").equals(target.getBigInteger()));
  assertTrue("Correct float2 value", new Float("8.1").equals(accessor.getPropertyValue("float2")));
  assertTrue("Correct float2 value", new Float("8.1").equals(target.getFloat2()));
  assertTrue("Correct double2 value", new Double("6.1").equals(accessor.getPropertyValue("double2")));
  assertTrue("Correct double2 value", new Double("6.1").equals(target.getDouble2()));
  assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(accessor.getPropertyValue("bigDecimal")));
  assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(target.getBigDecimal()));

代码示例来源:origin: knowm/XChange

public BigDecimal calculateFeeBtc() {
 return roundUp(amount.multiply(new BigDecimal(.5))).divide(new BigDecimal(100.));
}

代码示例来源: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: stackoverflow.com

public static BigDecimal sqrt(BigDecimal A, final int SCALE) {
  BigDecimal x0 = new BigDecimal("0");
  BigDecimal x1 = new BigDecimal(Math.sqrt(A.doubleValue()));
  while (!x0.equals(x1)) {
    x0 = x1;
    x1 = A.divide(x0, SCALE, ROUND_HALF_UP);
    x1 = x1.add(x0);
    x1 = x1.divide(TWO, SCALE, ROUND_HALF_UP);

  }
  return x1;
}

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

public static Optional<BigDecimal> fromMetastoreDecimal(@Nullable Decimal decimal)
{
  if (decimal == null) {
    return Optional.empty();
  }
  return Optional.of(new BigDecimal(new BigInteger(decimal.getUnscaled()), decimal.getScale()));
}

代码示例来源:origin: knowm/XChange

public BigDecimal getAveragePrice() {
 return low.add(high).divide(new BigDecimal("2"));
}

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

public BigDecimal getHitRate() {
    if (getRequestCount() == 0) {
      return new BigDecimal(-1);
    }
    BigDecimal percentage = new BigDecimal(getCacheHitCount()).divide(new BigDecimal(getRequestCount
        ()), 2, BigDecimal.ROUND_HALF_UP);
    percentage = percentage.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
    return percentage;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void parseBigDecimalNumber2() {
  String bigDecimalAsString = "0.001";
  Number bigDecimal = NumberUtils.parseNumber(bigDecimalAsString, BigDecimal.class);
  assertEquals(new BigDecimal(bigDecimalAsString), bigDecimal);
}

代码示例来源:origin: knowm/XChange

System.out.println("fetching data...");
System.out.println("orderBook = " + orderBook);
System.out.println("received data.");
System.out.println("plotting...");
BigDecimal accumulatedBidUnits = new BigDecimal("0");
for (LimitOrder limitOrder : orderBook.getBids()) {
 if (limitOrder.getLimitPrice().doubleValue() > 5000) {
  xData.add(limitOrder.getLimitPrice());
  accumulatedBidUnits = accumulatedBidUnits.add(limitOrder.getOriginalAmount());
  yData.add(accumulatedBidUnits);
BigDecimal accumulatedAskUnits = new BigDecimal("0");
for (LimitOrder limitOrder : orderBook.getAsks()) {
 if (limitOrder.getLimitPrice().doubleValue() < 20000) {
  xData.add(limitOrder.getLimitPrice());
  accumulatedAskUnits = accumulatedAskUnits.add(limitOrder.getOriginalAmount());
  yData.add(accumulatedAskUnits);

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

System.out.println(new BigDecimal(0.1));
System.out.println(new BigDecimal(x));

相关文章