}
result = high_result;
if (result.getType() == Formattable::kDouble) {
- int32_t r = (int32_t)result.getDouble();
- if ((double)r == result.getDouble()) {
- result.setLong(r);
+ double d = result.getDouble();
+ if (!uprv_isNaN(d) && d == uprv_trunc(d) && INT32_MIN <= d && d <= INT32_MAX) {
+ // Note: casting a double to an int when the double is too large or small
+ // to fit the destination is undefined behavior. The explicit range checks,
+ // above, are required. Just casting and checking the result value is undefined.
+ result.setLong(static_cast<int32_t>(d));
}
}
}