NumberUtils { public static boolean isLikelyFloat(int value) { if (value == canonicalFloatNaN || value == maxFloat || value == piFloat || value == eFloat) { return true; } if (value == Integer.MAX_VALUE || value == Integer.MIN_VALUE) { return false; } int packageId = value >> 24; int resourceType = value >> 16 & 0xff; int resourceId = value & 0xffff; if ((packageId == 0x7f || packageId == 1) && resourceType < 0x1f && resourceId < 0xfff) { return false; } float floatValue = Float.intBitsToFloat(value); if (Float.isNaN(floatValue)) { return false; } String asInt = format.format(value); String asFloat = format.format(floatValue); int decimalPoint = asFloat.indexOf('.'); int exponent = asFloat.indexOf("E"); int zeros = asFloat.indexOf("000"); if (zeros > decimalPoint && zeros < exponent) { asFloat = asFloat.substring(0, zeros) + asFloat.substring(exponent); } else { int nines = asFloat.indexOf("999"); if (nines > decimalPoint && nines < exponent) { asFloat = asFloat.substring(0, nines) + asFloat.substring(exponent); } } return asFloat.length() < asInt.length(); }  static boolean isLikelyFloat(int value); static boolean isLikelyDouble(long value);  }
@Test public void isLikelyFloatTest() { Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1.23f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1.0f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.NaN))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.POSITIVE_INFINITY))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1e-30f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1000f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(-1f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(-5f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1.3333f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(4.5f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(.1f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(50000f))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.MAX_VALUE))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits((float)Math.PI))); Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits((float)Math.E))); Assert.assertTrue(NumberUtils.isLikelyFloat(2139095039)); Assert.assertFalse(NumberUtils.isLikelyFloat(0)); Assert.assertFalse(NumberUtils.isLikelyFloat(1)); Assert.assertFalse(NumberUtils.isLikelyFloat(10)); Assert.assertFalse(NumberUtils.isLikelyFloat(100)); Assert.assertFalse(NumberUtils.isLikelyFloat(1000)); Assert.assertFalse(NumberUtils.isLikelyFloat(1024)); Assert.assertFalse(NumberUtils.isLikelyFloat(1234)); Assert.assertFalse(NumberUtils.isLikelyFloat(-5)); Assert.assertFalse(NumberUtils.isLikelyFloat(-13)); Assert.assertFalse(NumberUtils.isLikelyFloat(-123)); Assert.assertFalse(NumberUtils.isLikelyFloat(20000000)); Assert.assertFalse(NumberUtils.isLikelyFloat(2000000000)); Assert.assertFalse(NumberUtils.isLikelyFloat(-2000000000)); Assert.assertFalse(NumberUtils.isLikelyFloat(Integer.MAX_VALUE)); Assert.assertFalse(NumberUtils.isLikelyFloat(Integer.MIN_VALUE)); Assert.assertFalse(NumberUtils.isLikelyFloat(Short.MIN_VALUE)); Assert.assertFalse(NumberUtils.isLikelyFloat(Short.MAX_VALUE)); }