Java Code Examples for java.math.BigDecimal#byteValue()

The following examples show how to use java.math.BigDecimal#byteValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionEvaluator.java    From doma with Apache License 2.0 6 votes vote down vote up
protected Object narrowValue(BigDecimal value, Class<?> clazz) {
  if (clazz == BigDecimal.class) {
    return value;
  } else if (clazz == BigInteger.class) {
    return value.toBigInteger();
  } else if (clazz == Double.class || clazz == double.class) {
    return value.doubleValue();
  } else if (clazz == Float.class || clazz == float.class) {
    return value.floatValue();
  } else if (clazz == Long.class || clazz == long.class) {
    return value.longValue();
  } else if (clazz == Integer.class || clazz == int.class) {
    return value.intValue();
  } else if (clazz == Short.class || clazz == short.class) {
    return value.shortValue();
  } else if (clazz == Byte.class || clazz == byte.class) {
    return value.byteValue();
  }
  return assertUnreachable();
}
 
Example 2
Source File: TypeUtils.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 3
Source File: TypeUtils.java    From uavstack with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: DecimalToScaledFixedConverter.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public byte toByte(int index) throws SFException
{
  if (isNull(index))
  {
    return 0;
  }

  BigDecimal bigDecimal = toBigDecimal(index);
  if (bigDecimal.scale() == 0)
  {
    byte byteVal = bigDecimal.byteValue();

    if (byteVal == bigDecimal.longValue())
    {
      return byteVal;
    }
    else
    {
      throw new SFException(ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr,
                            "Byte", bigDecimal.toPlainString());
    }
  }
  else
  {
    throw new SFException(ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr,
                          "Byte", bigDecimal.toPlainString());
  }
}
 
Example 5
Source File: BinaryRowProtocol.java    From mariadb-connector-j with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Get byte from raw binary format.
 *
 * @param columnInfo column information
 * @return byte value
 * @throws SQLException if column type doesn't permit conversion
 */
public byte getInternalByte(ColumnDefinition columnInfo) throws SQLException {
  if (lastValueWasNull()) {
    return 0;
  }
  long value;
  switch (columnInfo.getColumnType()) {
    case BIT:
      value = parseBit();
      break;
    case TINYINT:
      value = getInternalTinyInt(columnInfo);
      break;
    case SMALLINT:
    case YEAR:
      value = getInternalSmallInt(columnInfo);
      break;
    case INTEGER:
    case MEDIUMINT:
      value = getInternalMediumInt(columnInfo);
      break;
    case BIGINT:
      value = getInternalLong(columnInfo);
      break;
    case FLOAT:
      value = (long) getInternalFloat(columnInfo);
      break;
    case DOUBLE:
      value = (long) getInternalDouble(columnInfo);
      break;
    case DECIMAL:
    case OLDDECIMAL:
      BigDecimal bigDecimal = getInternalBigDecimal(columnInfo);
      rangeCheck(Byte.class, Byte.MIN_VALUE, Byte.MAX_VALUE, bigDecimal, columnInfo);
      return bigDecimal.byteValue();
    case VARSTRING:
    case VARCHAR:
    case STRING:
      value = Long.parseLong(new String(buf, pos, length, StandardCharsets.UTF_8));
      break;
    default:
      throw new SQLException(
          "getByte not available for data field type "
              + columnInfo.getColumnType().getJavaTypeName());
  }
  rangeCheck(Byte.class, Byte.MIN_VALUE, Byte.MAX_VALUE, value, columnInfo);
  return (byte) value;
}
 
Example 6
Source File: SimpleNumberFormatter.java    From super-csv-annotation with Apache License 2.0 3 votes vote down vote up
private Number parseFromBigDecimal(final Class<? extends Number> type, final BigDecimal number) {
    
    if(Byte.class.isAssignableFrom(type) || byte.class.isAssignableFrom(type)) {
        return lenient ? number.byteValue() : number.byteValueExact();
        
    } else if(Short.class.isAssignableFrom(type) || short.class.isAssignableFrom(type)) {
        return lenient ? number.shortValue() : number.shortValueExact();
        
    } else if(Integer.class.isAssignableFrom(type) || int.class.isAssignableFrom(type)) {
        return lenient ? number.intValue() : number.intValueExact();
        
    } else if(Long.class.isAssignableFrom(type) || long.class.isAssignableFrom(type)) {
        return lenient ? number.longValue() : number.longValueExact();
        
    } else if(Float.class.isAssignableFrom(type) || float.class.isAssignableFrom(type)) {
        return number.floatValue();
        
    } else if(Double.class.isAssignableFrom(type) || double.class.isAssignableFrom(type)) {
        return number.doubleValue();
        
    } else if(type.isAssignableFrom(BigInteger.class)) {
        return lenient ? number.toBigInteger() : number.toBigIntegerExact();
        
    } else if(type.isAssignableFrom(BigDecimal.class)) {
        return number;
        
    }
    
    throw new IllegalArgumentException(String.format("Not support class type : %s", type.getCanonicalName()));
    
}
 
Example 7
Source File: NumberFormatWrapper.java    From super-csv-annotation with Apache License 2.0 3 votes vote down vote up
private Number convertWithBigDecimal(final Class<? extends Number> type, final BigDecimal number, final String str) {
    
    if(Byte.class.isAssignableFrom(type) || byte.class.isAssignableFrom(type)) {
        return lenient ? number.byteValue() : number.byteValueExact();
        
    } else if(Short.class.isAssignableFrom(type) || short.class.isAssignableFrom(type)) {
        return lenient ? number.shortValue() : number.shortValueExact();
        
    } else if(Integer.class.isAssignableFrom(type) || int.class.isAssignableFrom(type)) {
        return lenient ? number.intValue() : number.intValueExact();
        
    } else if(Long.class.isAssignableFrom(type) || long.class.isAssignableFrom(type)) {
        return lenient ? number.longValue() : number.longValueExact();
        
    } else if(Float.class.isAssignableFrom(type) || float.class.isAssignableFrom(type)) {
        return number.floatValue();
        
    } else if(Double.class.isAssignableFrom(type) || double.class.isAssignableFrom(type)) {
        return number.doubleValue();
        
    } else if(type.isAssignableFrom(BigInteger.class)) {
        return lenient ? number.toBigInteger() : number.toBigIntegerExact();
        
    } else if(type.isAssignableFrom(BigDecimal.class)) {
        return number;
        
    }
    
    throw new IllegalArgumentException(String.format("not support class type : %s", type.getCanonicalName()));
    
}