halfTowardZero static method
Rounds halves toward zero.
1.5 → 1, 2.5 → 2, -1.5 → -1, -2.5 → -2
Implementation
static double halfTowardZero(double value, {int precision = 2}) {
final factor = math.pow(10, precision);
final scaled = value * factor;
final integer = scaled.truncateToDouble();
final fraction = (scaled - integer).abs();
// Check if it's exactly a half
if ((fraction - 0.5).abs() < 1e-10) {
return integer / factor;
}
// Not a half, use standard rounding
return scaled.roundToDouble() / factor;
}