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

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

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

BigDecimal.byteValueExact介绍

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

代码示例

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

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

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

public static byte byteValue(BigDecimal decimal) {
  if (decimal == null) {
    return 0;
  }
  int scale = decimal.scale();
  if (scale >= -100 && scale <= 100) {
    return decimal.byteValue();
  }
  return decimal.byteValueExact();
}

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

bd = (BigDecimal) value;
try {
  bd.byteValueExact();
  return true;
} catch (ArithmeticException e) {

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

@Override
public Byte toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder,
  Integer maxLength, Integer scale) {
 if (l == 0) {
  return null;
 }
 if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE, PFloat.INSTANCE,
   PUnsignedFloat.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
   PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
   PUnsignedTinyint.INSTANCE)) {
  return actualType.getCodec().decodeByte(b, o, sortOrder);
 } else if (actualType == PDecimal.INSTANCE) {
  BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder);
  return bd.byteValueExact();
 }
 throwConstraintViolationException(actualType, this);
 return null;
}

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

bd = (BigDecimal) value;
try {
  bd.byteValueExact();
  return true;
} catch (ArithmeticException e) {

代码示例来源:origin: org.beanio/beanio

@Override
protected Byte createNumber(BigDecimal bg) throws ArithmeticException {
  return bg.byteValueExact();
}

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

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

代码示例来源:origin: org.javamoney.moneta/moneta-core

@Override
public <E extends Number> Byte convertExact(Class<E> numberType,
    Number number) {
  
  return ConvertBigDecimal.of(number).byteValueExact();
}

代码示例来源:origin: org.gradle/gradle-core

@Override
  protected void convertNumberToNumber(BigDecimal n, NotationConvertResult<? super Byte> result) {
    result.converted(n.byteValueExact());
  }
}

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

@SuppressWarnings("deprecation")
@Override
protected void set(BigDecimal value, ByteOption property) {
  property.modify(value.byteValueExact());
}

代码示例来源:origin: org.apache.marmotta/ldpath-core

@Override
public Byte transform(RDFBackend<Node> backend, Node node, Map<String, String> configuration) throws IllegalArgumentException {
  if(backend.isLiteral(node)) {
    return backend.decimalValue(node).byteValueExact();
  } else {
    throw new IllegalArgumentException("cannot transform node of type "+
      node.getClass().getCanonicalName()+" to byte");
  }
}

代码示例来源:origin: at.newmedialab.ldpath/ldpath-core-bundle

@Override
public Byte transform(RDFBackend<Node> backend, Node node) throws IllegalArgumentException {
  if(backend.isLiteral(node)) {
    return backend.decimalValue(node).byteValueExact();
  } else {
    throw new IllegalArgumentException("cannot transform node of type "+
      node.getClass().getCanonicalName()+" to byte");
  }
}

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

@Override
public Byte transform(RDFBackend<Node> backend, Node node, Map<String, String> configuration) throws IllegalArgumentException {
  if(backend.isLiteral(node)) {
    return backend.decimalValue(node).byteValueExact();
  } else {
    throw new IllegalArgumentException("cannot transform node of type "+
      node.getClass().getCanonicalName()+" to byte");
  }
}

代码示例来源:origin: com.aliyun.openservices/ons-client

public static Byte castToByte(Object value){
  if(value == null){
    return null;
  }
  if(value instanceof BigDecimal){
    return ((BigDecimal) value).byteValueExact();
  }
  if(value instanceof Number){
    return ((Number) value).byteValue();
  }
  if(value instanceof String){
    String strVal = (String) value;
    if(strVal.length() == 0 //
        || "null".equals(strVal) //
        || "NULL".equals(strVal)){
      return null;
    }
    return Byte.parseByte(strVal);
  }
  throw new JSONException("can not cast to byte, value : " + value);
}

代码示例来源:origin: com.eventsourcing/pgjdbc-ng

public static byte coerceToByte(Object val) throws SQLException {
 if (val == null) {
  return 0;
 }
 else if (val instanceof Byte) {
  return (byte) val;
 }
 else if (val instanceof Number) {
  try {
   return new BigDecimal(val.toString()).setScale(0, HALF_EVEN).byteValueExact();
  }
  catch (ArithmeticException e) {
   throw new SQLException("Coercion error", e);
  }
 }
 else if (val instanceof String) {
  return Byte.parseByte(((String) val).trim());
 }
 else if (val instanceof Boolean) {
  return ((Boolean) val) ? (byte) 1 : (byte) 0;
 }
 throw createCoercionException(val.getClass(), byte.class, val);
}

代码示例来源:origin: net.lecousin/core

/** Convert a BigDecimal into the specified type. */
public static void convertBigDecimalValue(BigDecimal n, Class<?> type, AsyncWork<Number, Exception> result) {
  try {
    if (byte.class.equals(type) || Byte.class.equals(type))
      result.unblockSuccess(Byte.valueOf(n.byteValueExact()));
    else if (short.class.equals(type) || Short.class.equals(type))
      result.unblockSuccess(Short.valueOf(n.shortValueExact()));
    else if (int.class.equals(type) || Integer.class.equals(type))
      result.unblockSuccess(Integer.valueOf(n.intValueExact()));
    else if (long.class.equals(type) || Long.class.equals(type))
      result.unblockSuccess(Long.valueOf(n.longValueExact()));
    else if (float.class.equals(type) || Float.class.equals(type))
      result.unblockSuccess(Float.valueOf(n.floatValue()));
    else if (double.class.equals(type) || Double.class.equals(type))
      result.unblockSuccess(Double.valueOf(n.doubleValue()));
    else if (BigInteger.class.equals(type))
      result.unblockSuccess(n.toBigIntegerExact());
    else if (BigDecimal.class.equals(type))
      result.unblockSuccess(n);
    else
      throw new Exception("Unknown numeric value type " + type.getName());
  } catch (Exception e) {
    result.error(e);
  }
}

代码示例来源:origin: org.beanfabrics/beanfabrics-core

public Byte getByte()
  throws ConversionException {
  if (isEmpty()) {
    return null;
  } else {
    try {
      return getBigDecimal().byteValueExact();
    } catch (ArithmeticException ex) {
      throw new ConversionException(ex);
    }
  }
}

代码示例来源:origin: org.beanfabrics/beanfabrics-core

public Byte getByte()
  throws ConversionException {
  if (isEmpty()) {
    return null;
  } else {
    try {
      return getBigDecimal().byteValueExact();
    } catch (ArithmeticException ex) {
      throw new ConversionException(ex);
    }
  }
}

代码示例来源:origin: com.aliyun.phoenix/ali-phoenix-core

@Override
public Byte toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder,
  Integer maxLength, Integer scale) {
 if (l == 0) {
  return null;
 }
 if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE, PFloat.INSTANCE,
   PUnsignedFloat.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
   PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
   PUnsignedTinyint.INSTANCE)) {
  return actualType.getCodec().decodeByte(b, o, sortOrder);
 } else if (actualType == PDecimal.INSTANCE) {
  BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder);
  return bd.byteValueExact();
 }
 throwConstraintViolationException(actualType, this);
 return null;
}

代码示例来源:origin: org.apache.phoenix/phoenix-core

@Override
public Byte toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder,
  Integer maxLength, Integer scale) {
 if (l == 0) {
  return null;
 }
 if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE, PFloat.INSTANCE,
   PUnsignedFloat.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
   PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
   PUnsignedTinyint.INSTANCE)) {
  return actualType.getCodec().decodeByte(b, o, sortOrder);
 } else if (actualType == PDecimal.INSTANCE) {
  BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder);
  return bd.byteValueExact();
 }
 throwConstraintViolationException(actualType, this);
 return null;
}

相关文章