BigDecimal is definitely the way to go here but, if you want another alternative you could do something like this....
- Convert the double type value to string;
- Remove the decimal point (whatever type it is);
- Convert the string to a long data type;
- See if the long value is greater than or equal to Integer.MIN_VALUE
or, less than or equal to Integer.MAX_VALUE. If it is, convert the
long value to an int. If it isn't then remove a single trailing digit
from the long value and check again. Keep checking until the long
value falls within the Integer.MIN_VALUE and Integer.MAX_VALUE range.
Here is a code example. Be sure to read the comments:
/* Double type values to convert in double type array.
You could potentially supply signed values as well.
The decimal point identifier can be whatever suits
your specific system locale (. or ,): */
double[] dblArray = {1.1234567896543217d, 1.123d, 1.1d,
1.1001, -1.10017887643456776d, 1.12345d, 0.12345d,
87.543276d, 4657.439802};
/* Display a table header in Console for the conversion results
from the double type array to int values. The String.format()
method is used: */
System.out.println("Remove decimal point and convert to int:\n");
System.out.println(String.format("%-25s %-12s", "Double (double)", "Integer (int)"));
System.out.println("=======================================");
/* Convert each double type element within the dblArray[] array
to int ans display the results in a Table style format: */
boolean greaterThanMinMaxInt; // Flag
for (Double dbl : dblArray) {
greaterThanMinMaxInt = false; // Used only for display purposes.
/* First convert the double type data to an long type value
by converting the double type value to String then removing
the decimal point (be it '.' or ',') then using the known
Long.parseLong() method to convert the new string value to
a long data type. This is done to ensure the the double
value conversion will fit and we can then check for Integer
MIN/MAX. */
long lng = Long.parseLong(String.valueOf(dbl).replaceAll("[.,]", ""));
/* Is the long type value less than Integer.MIN_VALUE or greater
than Integer.MAX_VALUE. If it is then remove a digit from the
trailing end of the long type value. Keep doing this until the
long value falls within bounds: */
while (lng < Integer.MIN_VALUE || lng > Integer.MAX_VALUE) {
lng = lng / 10; // Remove last digit in long value.
greaterThanMinMaxInt = true; // Flag set - Value needed reducing.
}
/* Now that the long type value is within Integer.MIN/MAX
values, convert the value to `int`: */
int intValue = (int) lng;
/* Display the result within Console Table. Nested Ternary
Operators are used to display which values were actually
reduced in length to satisfy Integer.MIN_VALUE and
Integer.MAX_VALUE: */
System.out.println(String.format("%-25s %-12s", String.valueOf(dbl),
String.valueOf(intValue) + (greaterThanMinMaxInt ? (intValue > 0
? " ('Had' Integer.MAX_VALUE Overflow)" : " ('Had' Integer.MIN_VALUE Overflow)")
: "")));
}
If you run the code above, you should see the following within the Console Window:
Remove decimal point and convert to int:
Double (double) Integer (int)
=======================================
1.1234567896543217 1123456789 ('Had' Integer.MAX_VALUE Overflow)
1.123 1123
1.1 11
1.1001 11001
-1.1001788764345677 -1100178876 ('Had' Integer.MIN_VALUE Overflow)
1.12345 112345
0.12345 12345
87.543276 87543276
4657.439802 465743980 ('Had' Integer.MAX_VALUE Overflow)