@Nonnull public static String formatValue(@Nonnull Object value) { if (value instanceof Double) { Double doubleValue = (Double) value; // 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 && DoubleMath.isMathematicalInteger(doubleValue)) { return Long.toString(doubleValue.longValue()) + ".00000"; } else { return String.format(Locale.US, "%1.5f", doubleValue); } } else { return value.toString(); } }
checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else { if (isMathematicalInteger(x)) { return x; } else {
checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else { if (isMathematicalInteger(x)) { return x; } else {
@GwtIncompatible // DoubleMath.isMathematicalInteger public void testIsMathematicalIntegerNotFinite() { for (double d : Arrays.asList(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN)) { assertFalse(DoubleMath.isMathematicalInteger(d)); } }
checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else { if (isMathematicalInteger(x)) { return x; } else {
@GwtIncompatible // DoubleMath.isMathematicalInteger public void testIsMathematicalIntegerIntegral() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { assertTrue(DoubleMath.isMathematicalInteger(d)); } }
@GwtIncompatible // DoubleMath.isMathematicalInteger public void testIsMathematicalIntegerFractional() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { assertFalse(DoubleMath.isMathematicalInteger(d)); } }
/** * 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)); }
void integerChecks() { checkArgument(Double.isInfinite(lowerBound) || DoubleMath.isMathematicalInteger(lowerBound)); checkArgument(Double.isInfinite(upperBound) || DoubleMath.isMathematicalInteger(upperBound)); } }
protected String format(double v) { if (v == 0) { return "0"; } if (DoubleMath.isMathematicalInteger(v)) { return Integer.toString((int) v); } return String.format("%.2f", v); }
static UnsignedLong toUnsignedLong(double value){ if(!DoubleMath.isMathematicalInteger(value)){ throw new IllegalArgumentException(); } return UnsignedLong.fromLongBits((long)value); }
/** * 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)); }
public static Number valueOf(double value) { return DoubleMath.isMathematicalInteger(value) ? valueOf((long) value) : value; } }
private static Object parseValue(String value) { if (value.equals("true") || value.equals("false")) { return Boolean.parseBoolean(value); } try { double doubleValue = Double.parseDouble(value); if (DoubleMath.isMathematicalInteger(doubleValue)) { long longValue = (long) doubleValue; int intValue = (int) longValue; if (longValue == intValue) { return intValue; } else { return longValue; } } return doubleValue; } catch (NumberFormatException e) { return value; } } }
/** * Returns the name from the wavelength. * * @param value The value to handle. * @return See above. */ private String getNameFromWavelength(Length value) { if (value == null) return null; //Check that the value is an int if (DoubleMath.isMathematicalInteger(value.getValue())) { return ""+value.getValue(); } return value.toString(); }
@Benchmark int isMathematicalInteger(int reps) { int tmp = 0; for (int i = 0; i < reps; i++) { int j = i & ARRAY_MASK; if (DoubleMath.isMathematicalInteger(doubles[j])) { tmp++; } } return tmp; }
static private Integer parseInteger(String value){ try { long result = Long.parseLong(value); return toInteger(result); } catch(NumberFormatException nfeInteger){ try { double result = Double.parseDouble(value); if(DoubleMath.isMathematicalInteger(result)){ return toInteger((long)result); } } catch(NumberFormatException nfeDouble){ // Ignored } // End try try { return toInteger(parseFlag(value)); } catch(IllegalArgumentException iae){ // Ignored } throw nfeInteger; } }
@GwtIncompatible // DoubleMath.isMathematicalInteger public void testIsMathematicalIntegerNotFinite() { for (double d : Arrays.asList(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN)) { assertFalse(DoubleMath.isMathematicalInteger(d)); } }
@GwtIncompatible // DoubleMath.isMathematicalInteger public void testIsMathematicalIntegerIntegral() { for (double d : INTEGRAL_DOUBLE_CANDIDATES) { assertTrue(DoubleMath.isMathematicalInteger(d)); } }
@GwtIncompatible // DoubleMath.isMathematicalInteger public void testIsMathematicalIntegerFractional() { for (double d : FRACTIONAL_DOUBLE_CANDIDATES) { assertFalse(DoubleMath.isMathematicalInteger(d)); } }