Java Code Examples for com.google.common.math.DoubleMath#isMathematicalInteger()

The following examples show how to use com.google.common.math.DoubleMath#isMathematicalInteger() . 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: MachineValueFormatter.java    From disthene-reader with MIT License 6 votes vote down vote up
@Override
protected String formatDoubleSpecialSmart(Double value) {
    BigDecimal bigDecimal = BigDecimal.valueOf(value);

    // do not do this for math integers
    if (!DoubleMath.isMathematicalInteger(value)) {
        // precision is just like in graphite (scale check redundant but let it be)
        if (bigDecimal.precision() > 12 && bigDecimal.scale() > 0) {
            int roundTo = bigDecimal.scale() - bigDecimal.precision() + 12 > 0 ? bigDecimal.scale() - bigDecimal.precision() + 12 : 0;
            bigDecimal = bigDecimal.setScale(roundTo, BigDecimal.ROUND_HALF_UP);
        }
    }


    return (bigDecimal.precision() + bigDecimal.scale() > 12) ?
            bigDecimal.stripTrailingZeros().toEngineeringString() : bigDecimal.stripTrailingZeros().toPlainString();

}
 
Example 2
Source File: AggregationFunctionUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
public static String formatValue(Object value) {
  if (value instanceof Double) {
    double doubleValue = (double) value;

    // NOTE: String.format() is very expensive, so avoid it for whole numbers that can fit in Long.
    //       We simply append ".00000" to long, in order to keep the existing behavior.
    if (doubleValue <= Long.MAX_VALUE && doubleValue >= Long.MIN_VALUE && DoubleMath
        .isMathematicalInteger(doubleValue)) {
      return (long) doubleValue + ".00000";
    } else {
      return String.format(Locale.US, "%1.5f", doubleValue);
    }
  } else {
    return value.toString();
  }
}
 
Example 3
Source File: RandomForestConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
static
UnsignedLong toUnsignedLong(double value){

	if(!DoubleMath.isMathematicalInteger(value)){
		throw new IllegalArgumentException();
	}

	return UnsignedLong.fromLongBits((long)value);
}
 
Example 4
Source File: GraphiteUtils.java    From disthene-reader with MIT License 5 votes vote down vote up
public static String formatDoubleSpecialPlain(Double value) {
    BigDecimal bigDecimal = BigDecimal.valueOf(value);

    // do not do this for math integers
    if (!DoubleMath.isMathematicalInteger(value)) {
        // precision is just like in graphite (scale check redundant but let it be)
        if (bigDecimal.precision() > 12 && bigDecimal.scale() > 0) {
            int roundTo = bigDecimal.scale() - bigDecimal.precision() + 12 > 0 ? bigDecimal.scale() - bigDecimal.precision() + 12 : 0;
            bigDecimal = bigDecimal.setScale(roundTo, BigDecimal.ROUND_HALF_UP);
        }
    }


    return bigDecimal.stripTrailingZeros().toPlainString();
}
 
Example 5
Source File: HumanValueFormatter.java    From disthene-reader with MIT License 5 votes vote down vote up
@Override
protected String formatDoubleSpecialSmart(Double value) {
    BigDecimal bigDecimal = BigDecimal.valueOf(value);

    // do not do this for math integers
    if (!DoubleMath.isMathematicalInteger(value)) {
        // precision is just like in graphite (scale check redundant but let it be)
        if (bigDecimal.precision() > 12 && bigDecimal.scale() > 0) {
            int roundTo = bigDecimal.scale() - bigDecimal.precision() + 12 > 0 ? bigDecimal.scale() - bigDecimal.precision() + 12 : 0;
            bigDecimal = bigDecimal.setScale(roundTo, BigDecimal.ROUND_HALF_UP);
        }
    }

    return formatter.format(bigDecimal.doubleValue());
}
 
Example 6
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void should_detect_integer() {
    boolean result1 = DoubleMath.isMathematicalInteger(2.0);
    assertThat(result1, equalTo(true));
    boolean result2 = DoubleMath.isMathematicalInteger(2.1);
    assertThat(result2, equalTo(false));
}
 
Example 7
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenMathematicalIntDouble_shouldReturnTrueIfInRange() {
    boolean result = DoubleMath.isMathematicalInteger(5);
    assertTrue(result);
}
 
Example 8
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenMathematicalIntDouble_shouldReturnFalseIfNotInRange() {
    boolean result = DoubleMath.isMathematicalInteger(5.2);
    assertFalse(result);
}
 
Example 9
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenIsPowerOfTwoDouble_shouldReturnTrueIfIsPowerOfTwo() {
    boolean result = DoubleMath.isMathematicalInteger(4);
    assertTrue(result);
}
 
Example 10
Source File: GuavaDoubleMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenIsPowerOfTwoDouble_shouldReturnFalseIsNotPowerOfTwoe() {
    boolean result = DoubleMath.isMathematicalInteger(5.2);
    assertFalse(result);
}
 
Example 11
Source File: CurrencyAmount.java    From Strata with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the amount as a string.
 * <p>
 * The format is the currency code, followed by a space, followed by the
 * amount: '${currency} ${amount}'.
 *
 * @return the currency amount
 */
@Override
@ToString
public String toString() {
  return currency + " " +
      (DoubleMath.isMathematicalInteger(amount) ? Long.toString((long) amount) : Double.toString(amount));
}
 
Example 12
Source File: FxRate.java    From Strata with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the formatted string version of the currency pair.
 * <p>
 * The format is '${baseCurrency}/${counterCurrency} ${rate}'.
 * 
 * @return the formatted string
 */
@Override
public String toString() {
  return pair + " " + (DoubleMath.isMathematicalInteger(rate) ? Long.toString((long) rate) : Double.toString(rate));
}