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

The following examples show how to use java.math.BigDecimal#floatValue() . These examples are extracted from open source projects. 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 Project: spork   File: FloatRoundTo.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a numeric value to round, a number of digits to keep, and an optional rounding mode.
 * @return output returns a single numeric value, the number with only those digits retained
 */
@Override
public Float exec(Tuple input) throws IOException {
    if (input == null || input.size() < 2)
        return null;

    try {
        Float        num    = (Float)input.get(0);
        Integer      digits = (Integer)input.get(1);
        RoundingMode mode   = (input.size() >= 3) ?
            RoundingMode.valueOf(DataType.toInteger(input.get(2))) : RoundingMode.HALF_EVEN;
        if (num == null) return null;

        BigDecimal bdnum  = BigDecimal.valueOf(num);
        bdnum = bdnum.setScale(digits, mode);
        return bdnum.floatValue();
    } catch (Exception e){
        throw new IOException("Caught exception processing input row ", e);
    }
}
 
Example 2
private static Object coerceDecimalToExpectedType(BigDecimal value, Types.MinorType fieldType)
{
    switch (fieldType) {
        case INT:
        case TINYINT:
        case SMALLINT:
            return value.intValue();
        case BIGINT:
            return value.longValue();
        case FLOAT4:
            return value.floatValue();
        case FLOAT8:
            return value.doubleValue();
        default:
            return value;
    }
}
 
Example 3
public static float drawSlider(final float value, final float min, final float max, final int x, final int y, final int width, final int mouseX, final int mouseY, final Color color) {
    final float displayValue = Math.max(min, Math.min(value, max));

    final float sliderValue = (float) x + (float) width * (displayValue - min) / (max - min);

    RenderUtils.drawRect(x, y, x + width, y + 2, Integer.MAX_VALUE);
    RenderUtils.drawRect(x, y, sliderValue, y + 2, color);
    RenderUtils.drawFilledCircle((int) sliderValue, y + 1, 3, color);

    if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + 3 && Mouse.isButtonDown(0)) {
        double i = MathHelper.clamp_double(((double) mouseX - (double) x) / ((double) width - 3), 0, 1);

        BigDecimal bigDecimal = new BigDecimal(Double.toString((min + (max - min) * i)));
        bigDecimal = bigDecimal.setScale(2, 4);
        return bigDecimal.floatValue();
    }

    return value;
}
 
Example 4
public static float getWeightPercentage(BigDecimal weight) {
	if (weight==null) {
		return 0.0f;
	}
	if (weight.floatValue() == 0.0f ) {
		return 0.0f;
	}
	return weight.floatValue() / 100.0f;
}
 
Example 5
Source Project: SqlFaker   File: RandomUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 根据单浮点型范围获取随机数,可以设置小数精度
 * @param start 起点值
 * @param end 终点值
 * @param precision 小数点精度(最大6位数)
 * @return [start, end]范围内的随机数
 */
public static float nextFloatRange(float start, float end, int precision) {
    checkNumberValid((double) start, (double) end);

    if (start == end) {
        return start;
    }

    // 整数部分的值
    int intValue = nextIntRange((int) start, (int) end);

    // 生成一个随机浮点数(共7位小数)
    BigDecimal bd = new BigDecimal(RANDOM.nextFloat());

    // 设置精度,向上取整
    bd = bd.setScale(precision, BigDecimal.ROUND_FLOOR);

    // 小数部分的值
    float floatValue = bd.floatValue();

    // 获取 整型 + 浮点型 的值
    float resultValue = intValue + floatValue;

    // 防止结果越界
    resultValue = (resultValue < start) ? start : resultValue;
    resultValue = (resultValue > end) ? end : resultValue;

    return resultValue;
}
 
Example 6
Source Project: phoenix   File: PFloat.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object toObject(Object object, PDataType actualType) {
  if (object == null) {
    return null;
  }
  float f;
  if (equalsAny(actualType, PFloat.INSTANCE, PUnsignedFloat.INSTANCE)) {
    return object;
  } else if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE)) {
    double d = (Double) object;
    if (Double.isNaN(d)
        || d == Double.POSITIVE_INFINITY
        || d == Double.NEGATIVE_INFINITY
        || (d >= -Float.MAX_VALUE && d <= Float.MAX_VALUE)) {
      return (float) d;
    } else {
      throw newIllegalDataException(
          actualType + " value " + d + " cannot be cast to Float without changing its value");
    }
  } else if (equalsAny(actualType, PLong.INSTANCE, PUnsignedLong.INSTANCE)) {
    f = (Long) object;
    return f;
  } else if (equalsAny(actualType, PInteger.INSTANCE, PUnsignedInt.INSTANCE)) {
    f = (Integer) object;
    return f;
  } else if (equalsAny(actualType, PTinyint.INSTANCE, PUnsignedTinyint.INSTANCE)) {
    f = (Byte) object;
    return f;
  } else if (equalsAny(actualType, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE)) {
    f = (Short) object;
    return f;
  } else if (actualType == PDecimal.INSTANCE) {
    BigDecimal dl = (BigDecimal) object;
    return dl.floatValue();
  }
  return throwConstraintViolationException(actualType, this);
}
 
Example 7
@SuppressFBWarnings("FE_FLOATING_POINT_EQUALITY")
private void addApprox(BigDecimal roundFloor) {
    double approxDouble = roundFloor.doubleValue();
    if (approxDouble != value && Math.abs(approxDouble - value) / value < 0.001) {
        approxSet.add(approxDouble);
    }
    float approxFloat = roundFloor.floatValue();
    if (Math.abs(approxFloat - value) / value < 0.001) {
        approxSet.add(approxFloat);
        approxSet.add((double) approxFloat);
    }
}
 
Example 8
/**
 * Float value of a small negative BigDecimal
 */
public static boolean testFloatValueMinusZero() {
    String a = 
   "-123809648392384754573567356745735.63567890295784902768787678287E-400";
    BigDecimal aNumber = new BigDecimal(a);
    int minusZero = -2147483648;
    float result = aNumber.floatValue();

    System.out.println("testFloatValueMinusZero: result is " + 
            Float.floatToIntBits(result) + " (" + minusZero + " expected)");
    return (Float.floatToIntBits(result) == minusZero);
 }
 
Example 9
private float applyScale( float number, IAvroOutputField outputField ) {
  if ( outputField.getScale() > 0 ) {
    BigDecimal bd = new BigDecimal( number );
    bd = bd.setScale( outputField.getScale(), BigDecimal.ROUND_HALF_UP );
    number = bd.floatValue();
  }
  return number;
}
 
Example 10
Source Project: huntbugs   File: RoughConstant.java    License: Apache License 2.0 5 votes vote down vote up
private void addApprox(BigDecimal roundFloor) {
    double approxDouble = roundFloor.doubleValue();
    if (approxDouble != value && Math.abs(approxDouble - value) / value < 0.001) {
        approxSet.add(approxDouble);
    }
    float approxFloat = roundFloor.floatValue();
    if (Math.abs(approxFloat - value) / value < 0.001) {
        approxSet.add(approxFloat);
        approxSet.add((double) approxFloat);
    }
}
 
Example 11
@Override
public float getFloat( int rowOffset ) throws InvalidAccessException {
  final float result;
  switch ( getType().getMinorType() ) {
    // 1. Regular type:
    case FLOAT4:
      result = innerAccessor.getFloat( rowOffset );
      break;

    // 2. Converted-from types:
    case INT:
      result = innerAccessor.getInt( rowOffset );
      break;
    case BIGINT:
      result = innerAccessor.getLong( rowOffset );
      break;
    case FLOAT8:
      final double value = innerAccessor.getDouble( rowOffset );
      if ( Float.MIN_VALUE <= value && value <= Float.MAX_VALUE ) {
        result = (float) value;
      } else {
        throw newOverflowException( "getFloat(...)",
                                    "Java double / SQL DOUBLE PRECISION",
                                    value );
      }
      break;
    case DECIMAL:
      final BigDecimal decimalValue = innerAccessor.getBigDecimal( rowOffset );
      final float tempFloat = decimalValue.floatValue();
      if ( Float.NEGATIVE_INFINITY == tempFloat || Float.POSITIVE_INFINITY == tempFloat) {
        throw newOverflowException( "getFloat(...)",
          "Java decimal / SQL DECIMAL PRECISION",
          tempFloat );
      } else {
        result = tempFloat;
      }
      break;
    // 3. Not-yet-converted and unconvertible types:
    default:
      result = innerAccessor.getInt( rowOffset );
      break;
  }
  return result;
}
 
Example 12
private float getReoundedMeasure(Float measure, int decimal) {
	BigDecimal bd = new BigDecimal(Float.toString(measure));
	bd = bd.setScale(decimal, BigDecimal.ROUND_HALF_UP);
	return bd.floatValue();
}
 
Example 13
private float getReoundedMeasure(Float measure, int decimal) {
	BigDecimal bd = new BigDecimal(Float.toString(measure));
	bd = bd.setScale(decimal, BigDecimal.ROUND_HALF_UP);
	return bd.floatValue();
}
 
Example 14
/**
 * Validates {@link ResultSet} against expected data.
 */
private void testValues(
    List<Object> expectedRow,
    List<Object> resultRow,
    int rowNum) {
  if (expectedRow.size() != resultRow.size()) {
    dumpData(
        "Row " + rowNum + " Expected " + expected.size()
        + " columns, got " + result.size());
  }

  Iterator expectedIter = expectedRow.iterator();
  Iterator resultIter = resultRow.iterator();

  int colNum = 1;
  while (expectedIter.hasNext() && resultIter.hasNext()) {
    Object expectedValue = expectedIter.next();
    Object resultValue = resultIter.next();

    if ((expectedValue == null)
        || (expectedValue instanceof String)
        || (expectedValue instanceof Boolean)) {
      test(expectedValue, resultValue, rowNum, colNum);
    } else if (expectedValue instanceof BigInteger) {
      BigInteger expectedInt = (BigInteger) expectedValue;

      if (expectedInt.bitLength() <= 31) {
        test(
            expectedInt.intValue(),
            ((Number) resultValue).intValue(),
            rowNum,
            colNum);
      } else if (expectedInt.bitLength() <= 63) {
        test(
            expectedInt.longValue(),
            ((Number) resultValue).longValue(),
            rowNum,
            colNum);
      } else {
        // REVIEW: how do we return very
        // large integer values?
        test(expectedInt, resultValue, rowNum, colNum);
      }
    } else if (expectedValue instanceof BigDecimal) {
      BigDecimal expectedReal = (BigDecimal) expectedValue;

      float asFloat = expectedReal.floatValue();
      double asDouble = expectedReal.doubleValue();

      if ((asFloat != Float.POSITIVE_INFINITY)
          && (asFloat != Float.NEGATIVE_INFINITY)) {
        test(
            asFloat,
            ((Number) resultValue).floatValue(),
            rowNum,
            colNum);
      } else if (
          (asDouble != Double.POSITIVE_INFINITY)
              && (asDouble != Double.NEGATIVE_INFINITY)) {
        test(
            asDouble,
            ((Number) resultValue).doubleValue(),
            rowNum,
            colNum);
      } else {
        // REVIEW: how do we return very large decimal
        // values?
        test(expectedReal, resultValue, rowNum, colNum);
      }
    } else {
      throw new IllegalStateException(
          "unknown type of expected value: "
          + expectedValue.getClass().getName());
    }

    colNum++;
  }
}
 
Example 15
public float getFloat() throws SQLException {
    BigDecimal value = getBigDecimal();
    if (value == null) return FLOAT_NULL_VALUE;

    return value.floatValue();
}
 
Example 16
Source Project: ldparteditor   File: WarningFixer.java    License: MIT License 4 votes vote down vote up
private static boolean hasGoodDeterminant(String line) {
    String[] data_segments = line.trim().split("\\s+"); //$NON-NLS-1$
    // [ERROR] Check singularity
    Matrix4f tMatrix = new Matrix4f();
    float det = 0;
    try {
        // Offset
        BigDecimal M30 = new BigDecimal(data_segments[2]);
        tMatrix.m30 = M30.floatValue() * 1000f;
        BigDecimal M31 = new BigDecimal(data_segments[3]);
        tMatrix.m31 = M31.floatValue() * 1000f;
        BigDecimal M32 = new BigDecimal(data_segments[4]);
        tMatrix.m32 = M32.floatValue() * 1000f;
        // First row
        BigDecimal M00 = new BigDecimal(data_segments[5]);
        tMatrix.m00 = M00.floatValue();
        BigDecimal M10 = new BigDecimal(data_segments[6]);
        tMatrix.m10 = M10.floatValue();
        BigDecimal M20 = new BigDecimal(data_segments[7]);
        tMatrix.m20 = M20.floatValue();
        // Second row
        BigDecimal M01 = new BigDecimal(data_segments[8]);
        tMatrix.m01 = M01.floatValue();
        BigDecimal M11 = new BigDecimal(data_segments[9]);
        tMatrix.m11 = M11.floatValue();
        BigDecimal M21 = new BigDecimal(data_segments[10]);
        tMatrix.m21 = M21.floatValue();
        // Third row
        BigDecimal M02 = new BigDecimal(data_segments[11]);
        tMatrix.m02 = M02.floatValue();
        BigDecimal M12 = new BigDecimal(data_segments[12]);
        tMatrix.m12 = M12.floatValue();
        BigDecimal M22 = new BigDecimal(data_segments[13]);
        tMatrix.m22 = M22.floatValue();
    } catch (NumberFormatException nfe) {
        // Can't happen
        return false;
    }
    tMatrix.m33 = 1f;
    det = tMatrix.determinant();
    return Math.abs(det) >= Threshold.singularity_determinant;
}
 
Example 17
public static float round(float d, int decimalPlace) {
	BigDecimal bd = new BigDecimal(Float.toString(d));
	bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
	return bd.floatValue();
}
 
Example 18
Source Project: ldparteditor   File: VM04Rectifier.java    License: MIT License 4 votes vote down vote up
private boolean hasGoodDeterminant(String line) {
    String[] data_segments = line.trim().split("\\s+"); //$NON-NLS-1$
    // [ERROR] Check singularity
    Matrix4f tMatrix = new Matrix4f();
    float det = 0;
    try {
        // Offset
        BigDecimal M30 = new BigDecimal(data_segments[2]);
        tMatrix.m30 = M30.floatValue() * 1000f;
        BigDecimal M31 = new BigDecimal(data_segments[3]);
        tMatrix.m31 = M31.floatValue() * 1000f;
        BigDecimal M32 = new BigDecimal(data_segments[4]);
        tMatrix.m32 = M32.floatValue() * 1000f;
        // First row
        BigDecimal M00 = new BigDecimal(data_segments[5]);
        tMatrix.m00 = M00.floatValue();
        BigDecimal M10 = new BigDecimal(data_segments[6]);
        tMatrix.m10 = M10.floatValue();
        BigDecimal M20 = new BigDecimal(data_segments[7]);
        tMatrix.m20 = M20.floatValue();
        // Second row
        BigDecimal M01 = new BigDecimal(data_segments[8]);
        tMatrix.m01 = M01.floatValue();
        BigDecimal M11 = new BigDecimal(data_segments[9]);
        tMatrix.m11 = M11.floatValue();
        BigDecimal M21 = new BigDecimal(data_segments[10]);
        tMatrix.m21 = M21.floatValue();
        // Third row
        BigDecimal M02 = new BigDecimal(data_segments[11]);
        tMatrix.m02 = M02.floatValue();
        BigDecimal M12 = new BigDecimal(data_segments[12]);
        tMatrix.m12 = M12.floatValue();
        BigDecimal M22 = new BigDecimal(data_segments[13]);
        tMatrix.m22 = M22.floatValue();
    } catch (NumberFormatException nfe) {
        // Can't happen
        return false;
    }
    tMatrix.m33 = 1f;
    det = tMatrix.determinant();
    return Math.abs(det) >= Threshold.singularity_determinant;
}
 
Example 19
Source Project: tabula-java   File: Utils.java    License: MIT License 4 votes vote down vote up
public static float round(double d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}
 
Example 20
Source Project: sinavi-jfw   File: FloatConverter.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * {@link BigDecimal}を変換します。
 * @param bd 変換対象
 * @return 変換後
 */
public Float convert(BigDecimal bd) {
    return bd.floatValue();
}