本文整理了Java中java.math.BigDecimal.intValueExact()
方法的一些代码示例,展示了BigDecimal.intValueExact()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigDecimal.intValueExact()
方法的具体详情如下:
包路径:java.math.BigDecimal
类名称:BigDecimal
方法名:intValueExact
[英]Returns this BigDecimal as a int value if it has no fractional part and if its value fits to the int range ([-231..231-1]). If these conditions are not met, an ArithmeticException is thrown.
[中]如果此BigDecimal没有小数部分,并且其值符合int范围([-231..231-1]),则将其作为int值返回。如果不满足这些条件,将抛出算术异常。
代码示例来源:origin: stackoverflow.com
long aLong = ...;
int anInt = new BigDecimal(aLong).intValueExact(); // throws ArithmeticException
// if outside bounds
代码示例来源:origin: com.alibaba/fastjson
public static int intValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.intValue();
}
return decimal.intValueExact();
}
代码示例来源:origin: graphql-java/graphql-java
private Integer convertImpl(Object input) {
if (input instanceof Integer) {
return (Integer) input;
} else if (isNumberIsh(input)) {
BigDecimal value;
try {
value = new BigDecimal(input.toString());
} catch (NumberFormatException e) {
return null;
}
try {
return value.intValueExact();
} catch (ArithmeticException e) {
return null;
}
} else {
return null;
}
}
代码示例来源:origin: apache/storm
private Integer getInteger(SqlNode n) {
return n == null ? null : ((BigDecimal) SqlLiteral.value(n)).intValueExact();
}
代码示例来源:origin: apache/hive
@Override
protected HiveIntervalDayTime getIntervalDayTime(String arg) {
BigDecimal bd = new BigDecimal(arg);
BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger());
BigDecimal bdNanos = bd.subtract(bdSeconds);
return new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(),
bdNanos.multiply(NANOS_PER_SEC_BD).intValue());
}
}
代码示例来源:origin: wildfly/wildfly
@Override
public int intValueExact() {
return bigDecimalValue().intValueExact();
}
代码示例来源:origin: apache/drill
@Override
protected HiveIntervalDayTime getIntervalDayTime(String arg) {
BigDecimal bd = new BigDecimal(arg);
BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger());
BigDecimal bdNanos = bd.subtract(bdSeconds);
return new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(),
bdNanos.multiply(NANOS_PER_SEC_BD).intValue());
}
}
代码示例来源:origin: stackoverflow.com
int signOf2 = n2.signum();
try {
// Perform X^(A+B)=X^A*X^B (B = remainder)
double dn1 = n1.doubleValue();
// Compare the same row of digits according to context
if (!CalculatorUtils.isEqual(n1, dn1))
throw new Exception(); // Cannot convert n1 to double
n2 = n2.multiply(new BigDecimal(signOf2)); // n2 is now positive
BigDecimal remainderOf2 = n2.remainder(BigDecimal.ONE);
BigDecimal n2IntPart = n2.subtract(remainderOf2);
// Calculate big part of the power using context -
// bigger range and performance but lower accuracy
BigDecimal intPow = n1.pow(n2IntPart.intValueExact(),
CalculatorConstants.DEFAULT_CONTEXT);
BigDecimal doublePow =
new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue()));
result = intPow.multiply(doublePow);
} catch (Exception e) {
if (e instanceof CalculatorException)
throw (CalculatorException) e;
throw new CalculatorException(
CalculatorConstants.Errors.UNSUPPORTED_NUMBER_ +
"power!");
}
// Fix negative power
if (signOf2 == -1)
result = BigDecimal.ONE.divide(result, CalculatorConstants.BIG_SCALE,
RoundingMode.HALF_UP);
代码示例来源:origin: plantuml/plantuml
public BigDecimal eval(Number vv1, Number vv2) {
BigDecimal v1 = (BigDecimal) vv1;
BigDecimal v2 = (BigDecimal) vv2;
assertNotNull(v1, v2);
/*-
* Thanks to Gene Marin:
* http://stackoverflow.com/questions/3579779/how-to-do-a-fractional-power-on-bigdecimal-in-java
*/
int signOf2 = ((BigDecimal) v2).signum();
double dn1 = v1.doubleValue();
v2 = v2.multiply(new BigDecimal(signOf2)); // n2 is now positive
BigDecimal remainderOf2 = v2.remainder(BigDecimal.ONE);
BigDecimal n2IntPart = v2.subtract(remainderOf2);
BigDecimal intPow = v1.pow(n2IntPart.intValueExact(), mc);
BigDecimal doublePow = new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue()));
BigDecimal result = intPow.multiply(doublePow, mc);
if (signOf2 == -1) {
result = BigDecimal.ONE.divide(result, mc.getPrecision(), RoundingMode.HALF_UP);
}
return result;
}
});
代码示例来源:origin: square/moshi
@Override public int nextInt() throws IOException {
Object peeked = require(Object.class, Token.NUMBER);
int result;
if (peeked instanceof Number) {
result = ((Number) peeked).intValue();
} else if (peeked instanceof String) {
try {
result = Integer.parseInt((String) peeked);
} catch (NumberFormatException e) {
try {
BigDecimal asDecimal = new BigDecimal((String) peeked);
result = asDecimal.intValueExact();
} catch (NumberFormatException e2) {
throw typeMismatch(peeked, Token.NUMBER);
}
}
} else {
throw typeMismatch(peeked, Token.NUMBER);
}
remove();
return result;
}
代码示例来源:origin: stackoverflow.com
try { return bigDec.intValueExact(); }
catch(ArithmeticException e) {}
return bigDec.longValue();
代码示例来源:origin: com.alibaba/fastjson
lineNumber = 0;
} else if (value instanceof BigDecimal) {
lineNumber = ((BigDecimal) value).intValueExact();
} else{
lineNumber = value.intValue();
代码示例来源:origin: immutables/immutables
@Override
public int nextInt() throws IOException {
switch (delegate.getCurrentBsonType()) {
case DOUBLE:
return (int) delegate.readDouble();
case INT64:
return (int) delegate.readInt64();
case DECIMAL128:
return delegate.readDecimal128().bigDecimalValue().intValueExact();
default:
return delegate.readInt32();
}
}
代码示例来源:origin: apache/hive
BigDecimal bdNanos = bd.subtract(bdSeconds);
return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo,
new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(),
bdNanos.multiply(NANOS_PER_SEC_BD).intValue()));
default:
代码示例来源:origin: apache/drill
BigDecimal bdNanos = bd.subtract(bdSeconds);
return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo,
new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(),
bdNanos.multiply(NANOS_PER_SEC_BD).intValue()));
default:
代码示例来源:origin: org.glassfish/javax.json
@Override
public int intValueExact() {
return bigDecimalValue().intValueExact();
}
代码示例来源:origin: org.eclipse.jetty/jetty-webapp
throw new IllegalStateException ("Max session-timeout in minutes is "+org.eclipse.jetty.server.session.SessionHandler.MAX_INACTIVE_MINUTES);
context.getSessionHandler().setMaxInactiveInterval(asDecimal.intValueExact() * 60);
代码示例来源:origin: forcedotcom/phoenix
bd = (BigDecimal) value;
try {
bd.intValueExact();
return true;
} catch (ArithmeticException e) {
代码示例来源:origin: apache/phoenix
@Override
public Integer toObject(byte[] b, int o, int l, PDataType actualType,
SortOrder sortOrder, Integer maxLength, Integer scale) {
if (l == 0) {
return null;
}
if (equalsAny(actualType, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
PUnsignedTinyint.INSTANCE, PFloat.INSTANCE, PUnsignedFloat.INSTANCE, PDouble.INSTANCE,
PUnsignedDouble.INSTANCE)) {
return actualType.getCodec().decodeInt(b, o, sortOrder);
} else if (actualType == PDecimal.INSTANCE) {
BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder);
return bd.intValueExact();
}
throwConstraintViolationException(actualType, this);
return null;
}
代码示例来源:origin: alexa/alexa-skills-kit-sdk-for-java
public static ViewportProfile getViewportProfile(RequestEnvelope requestEnvelope) {
ViewportState viewportState = requestEnvelope.getContext().getViewport();
Shape shape = viewportState.getShape();
int currentPixelWidth = viewportState.getCurrentPixelWidth().intValueExact();
int currentPixelHeight = viewportState.getCurrentPixelHeight().intValueExact();
int dpi = viewportState.getDpi().intValueExact();
Orientation orientation = getOrientation(currentPixelHeight, currentPixelWidth);
内容来源于网络,如有侵权,请联系作者删除!