-
-
Save dkuppitz/e589e43763a3546586b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumberHelper { | |
static Class<? extends Number> getHighestNumberClass(final Number... numbers) { | |
int bits = 8; | |
boolean fp = false; | |
for (final Number number : numbers) { | |
int b; | |
switch (number.getClass().getSimpleName()) { | |
case "Short": b = 16; break; | |
case "Integer": b = 32; break; | |
case "Long": b = 64; break; | |
case "Float": fp = true; b = 32; break; | |
case "Double": fp = true; b = 64; break; | |
default: b = 8; break; | |
} | |
bits = Math.max(bits, b); | |
} | |
if (fp) { | |
if (bits <= 32) { | |
return Float.class; | |
} else if ( bits <= 64) { | |
return Double.class; | |
} else { | |
return BigDecimal.class; | |
} | |
} else { | |
if (bits <= 8) { | |
return Byte.class; | |
} else if ( bits <= 16) { | |
return Short.class; | |
} else if ( bits <= 32) { | |
return Integer.class; | |
} else if ( bits <= 64) { | |
return Long.class; | |
} else { | |
return BigInteger.class; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment