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

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

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

BigDecimal.longValueExact介绍

[英]Returns this BigDecimal as a long value if it has no fractional part and if its value fits to the int range ([-263..263-1]). If these conditions are not met, an ArithmeticException is thrown.
[中]如果此BigDecimal没有小数部分并且其值适合整数范围([-263..263-1]),则将其作为长值返回。如果不满足这些条件,将抛出算术异常。

代码示例

代码示例来源:origin: com.alibaba/fastjson

public static long longExtractValue(Number number) {
  if (number instanceof BigDecimal) {
    return ((BigDecimal) number).longValueExact();
  }
  return number.longValue();
}

代码示例来源:origin: com.alibaba/fastjson

public static long longValue(BigDecimal decimal) {
  if (decimal == null) {
    return 0;
  }
  int scale = decimal.scale();
  if (scale >= -100 && scale <= 100) {
    return decimal.longValue();
  }
  return decimal.longValueExact();
}

代码示例来源:origin: graphql-java/graphql-java

private Long convertImpl(Object input) {
  if (input instanceof Long) {
    return (Long) input;
  } else if (isNumberIsh(input)) {
    BigDecimal value;
    try {
      value = new BigDecimal(input.toString());
    } catch (NumberFormatException e) {
      return null;
    }
    try {
      return value.longValueExact();
    } catch (ArithmeticException e) {
      return null;
    }
  } else {
    return null;
  }
}

代码示例来源:origin: kiegroup/optaplanner

public static long parseMicroCost(String costString) {
  BigDecimal costBigDecimal = new BigDecimal(costString);
  if (costBigDecimal.scale() > 11) {
    throw new IllegalArgumentException("The costString (" + costString + ") has a scale ("
        + costBigDecimal.scale() + ") higher than 10.");
  }
  costBigDecimal = costBigDecimal.setScale(11);
  return costBigDecimal.multiply(MICROS_PER_ONE_AS_BIG_DECIMAL).longValueExact();
}

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

@HiveDecimalVersionV1
 public long longValueExact() {
  return bd.longValueExact();
 }
}

代码示例来源:origin: kiegroup/optaplanner

protected long parsePercentageMillis(String token) {
  BigDecimal millis;
  if (token.endsWith("%")) {
    millis = new BigDecimal(token.substring(0, token.length() - 1)).multiply(new BigDecimal(10L));
  } else {
    millis = new BigDecimal(token).multiply(new BigDecimal(1000L));
  }
  return millis.longValueExact();
}

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

@Override
public long longValueExact() {
  return bigDecimalValue().longValueExact();
}

代码示例来源:origin: apache/incubator-druid

return convertedBD.longValueExact();

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

if (object instanceof BigDecimal) {
  if (((BigDecimal) object).scale() <= 0) {
    object = ((BigDecimal) object).longValueExact();

代码示例来源:origin: square/moshi

@Override public long nextLong() throws IOException {
 Object peeked = require(Object.class, Token.NUMBER);
 long result;
 if (peeked instanceof Number) {
  result = ((Number) peeked).longValue();
 } else if (peeked instanceof String) {
  try {
   result = Long.parseLong((String) peeked);
  } catch (NumberFormatException e) {
   try {
    BigDecimal asDecimal = new BigDecimal((String) peeked);
    result = asDecimal.longValueExact();
   } catch (NumberFormatException e2) {
    throw typeMismatch(peeked, Token.NUMBER);
   }
  }
 } else {
  throw typeMismatch(peeked, Token.NUMBER);
 }
 remove();
 return result;
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * Check whether value represents a whole number
 *
 * @param value
 * @return
 */
private static boolean isInt( Number value ) {
 if ( value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte
   || value instanceof BigInteger ) {
  return true;
 }
 final BigDecimal bigDecimalValue;
 if ( value instanceof Double || value instanceof Float ) {
  bigDecimalValue = new BigDecimal( value.toString() );
 } else if ( value instanceof BigDecimal ) {
  bigDecimalValue = (BigDecimal) value;
 } else {
  throw new IllegalArgumentException( "Unexpected dataType: " + value.getClass().getName() );
 }
 try {
  bigDecimalValue.longValueExact();
  return true;
 } catch ( ArithmeticException e ) {
  return false;
 }
}

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

@Override
public long nextLong() throws IOException {
 switch (delegate.getCurrentBsonType()) {
 case DOUBLE:
  return (long) delegate.readDouble();
 case INT32:
  return delegate.readInt32();
 case DECIMAL128:
  return delegate.readDecimal128().bigDecimalValue().longValueExact();
 default:
  return delegate.readInt64();
 }
}

代码示例来源:origin: square/moshi

try {
 BigDecimal asDecimal = new BigDecimal(peekedString);
 result = asDecimal.longValueExact();
} catch (NumberFormatException | ArithmeticException e) {
 throw new JsonDataException("Expected a long but was " + peekedString

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

double value = 1234567.9;
long l = BigDecimal.valueOf(value)
          .setScale(0, RoundingMode.HALF_EVEN)
          .longValueExact(); // 1234568

double value = 99999999999999999999999999999999.9;
long l = BigDecimal.valueOf(value)
          .setScale(0, RoundingMode.HALF_EVEN)
          .longValueExact(); // ArithmeticException

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

long l = new BigDecimal("-9223372036854775808.2")
          .setScale(0, RoundingMode.HALF_EVEN)
          .longValueExact(); // ok, -9223372036854775808

long l = new BigDecimal("-9223372036854775808.9")
          .setScale(0, RoundingMode.HALF_EVEN)
          .longValueExact(); // ArithmeticException

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

@Override
  protected Value<Long> toDatastore(final BigDecimal value) {
    return LongValue.of(value.multiply(factor).longValueExact());
  }
}

代码示例来源:origin: org.glassfish/javax.json

@Override
public long longValueExact() {
  return bigDecimalValue().longValueExact();
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@UserFunction
@Description("apoc.number.exact.toExact(number) - return the exact value")
public Long toExact(@Name("number")Long number){
  if(number == null) return null;
  return new BigDecimal(number).longValueExact();
}

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

public LiteralParseNode wholeNumber(String text) {
  int length = text.length();
  // We know it'll fit into long, might still fit into int
  if (length <= PDataType.LONG_PRECISION-1) {
    long l = Long.parseLong(text);
    if (l <= Integer.MAX_VALUE) {
      // Fits into int
      return new LiteralParseNode((int)l);
    }
    return new LiteralParseNode(l);
  }
  // Might still fit into long
  BigDecimal d = new BigDecimal(text, PDataType.DEFAULT_MATH_CONTEXT);
  if (d.compareTo(MAX_LONG) <= 0) {
    return new LiteralParseNode(d.longValueExact());
  }
  // Doesn't fit into long
  return new LiteralParseNode(d);
}

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

@Override
public java.sql.Time toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder,
  Integer maxLength, Integer scale) {
 if (l == 0) {
  return null;
 }
 if (equalsAny(actualType, PTimestamp.INSTANCE, PUnsignedTimestamp.INSTANCE, PDate.INSTANCE,
   PUnsignedDate.INSTANCE, PTime.INSTANCE, PUnsignedTime.INSTANCE, PLong.INSTANCE,
   PUnsignedLong.INSTANCE)) {
  return new java.sql.Time(actualType.getCodec().decodeLong(b, o, sortOrder));
 } else if (actualType == PDecimal.INSTANCE) {
  BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder);
  return new java.sql.Time(bd.longValueExact());
 }
 throwConstraintViolationException(actualType, this);
 return null;
}

相关文章