diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/util/Rational.java b/tools/cldr-code/src/main/java/org/unicode/cldr/util/Rational.java index 30a0ebdb456..925f441f219 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/util/Rational.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/util/Rational.java @@ -32,7 +32,8 @@ * * @author markdavis */ -public final class Rational implements Comparable { +public final class Rational extends Number implements Comparable { + private static final long serialVersionUID = 1L; private static final Pattern INT_POWER_10 = Pattern.compile("10*"); public final BigInteger numerator; public final BigInteger denominator; @@ -298,17 +299,24 @@ public BigDecimal toBigDecimal(MathContext mathContext) { } } + @Override public double doubleValue() { if (denominator.equals(BigInteger.ZERO) && numerator.equals(BigInteger.ZERO)) { return Double.NaN; } - return new BigDecimal(numerator) - .divide(new BigDecimal(denominator), MathContext.DECIMAL64) - .doubleValue(); + return toBigDecimal(MathContext.DECIMAL64).doubleValue(); + } + + @Override + public float floatValue() { + if (denominator.equals(BigInteger.ZERO) && numerator.equals(BigInteger.ZERO)) { + return Float.NaN; + } + return toBigDecimal(MathContext.DECIMAL32).floatValue(); } public BigDecimal toBigDecimal() { - return toBigDecimal(MathContext.UNLIMITED); + return toBigDecimal(MathContext.DECIMAL128); // prevent failures due to repeating fractions } public static Rational of(BigDecimal bigDecimal) { @@ -736,4 +744,14 @@ public boolean approximatelyEquals(Rational b) { public boolean approximatelyEquals(Number b) { return approximatelyEquals(Rational.of(b.doubleValue()), EPSILON); } + + @Override + public int intValue() { + return toBigDecimal().intValue(); + } + + @Override + public long longValue() { + return toBigDecimal().longValue(); + } }