[Bug #1059] Fix bug
Fractions can overflow if the numerator and the denominators they carry are big. In that case big decimals are used instead. FEA: ItEr74S04BugFixing
This commit is contained in:
parent
955c39181f
commit
f2eb219ce2
2 changed files with 22 additions and 9 deletions
|
|
@ -24,6 +24,9 @@
|
|||
*/
|
||||
package org.zkoss.ganttz;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import org.apache.commons.lang.math.Fraction;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Duration;
|
||||
|
|
@ -67,8 +70,18 @@ public class DatesMapperOnInterval implements IDatesMapper {
|
|||
}
|
||||
|
||||
private int toPixels(Fraction proportion) {
|
||||
return proportion.multiplyBy(Fraction.getFraction(horizontalSize, 1))
|
||||
.intValue();
|
||||
try {
|
||||
return proportion.multiplyBy(Fraction.getFraction(horizontalSize, 1))
|
||||
.intValue();
|
||||
} catch (ArithmeticException e) {
|
||||
double d = Math.log10(horizontalSize);
|
||||
int scale = (int) d + 1;
|
||||
BigDecimal quotient = new BigDecimal(proportion.getNumerator())
|
||||
.divide(new BigDecimal(proportion.getDenominator()), scale,
|
||||
RoundingMode.HALF_UP);
|
||||
return quotient.multiply(new BigDecimal(horizontalSize))
|
||||
.intValue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -82,9 +82,13 @@ public class Interval {
|
|||
public Fraction getProportion(DateTime date) {
|
||||
Days fromStartToDate = Days.daysBetween(startInclusive,
|
||||
date.toLocalDate());
|
||||
Fraction fraction = Fraction.getFraction(fromStartToDate.getDays(),
|
||||
Fraction result = Fraction.getFraction(fromStartToDate.getDays(),
|
||||
this.daysBetween.getDays());
|
||||
return fraction.add(inTheDayIncrement(date));
|
||||
try {
|
||||
return result.add(inTheDayIncrement(date));
|
||||
} catch (ArithmeticException e) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private Fraction inTheDayIncrement(DateTime date) {
|
||||
|
|
@ -92,11 +96,7 @@ public class Interval {
|
|||
Duration duration = new Duration(atStartOfDay, date);
|
||||
double result = ((double) duration.getMillis())
|
||||
/ lengthBetween.getMillis();
|
||||
try {
|
||||
return Fraction.getFraction(result);
|
||||
} catch (ArithmeticException e) {
|
||||
return Fraction.ZERO;
|
||||
}
|
||||
return Fraction.getFraction(result);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue