79324650

Date: 2025-01-02 18:33:57
Score: 0.5
Natty:
Report link
public static long[] toFraction(double number) {
    final BigDecimal bigDecimal = BigDecimal.valueOf(number);
    final int decimalPlaces = bigDecimal.scale();
    final BigDecimal divisor = BigDecimal.TEN.pow(decimalPlaces);
    final BigDecimal dividend = bigDecimal.movePointRight(decimalPlaces);
    final BigInteger divisorInt = BigInteger.valueOf(divisor.longValue());
    final BigInteger dividendInt = BigInteger.valueOf(dividend.longValue());
    final BigInteger greatestCommonDivisor = divisorInt.gcd(dividendInt);
    return new long[] {
            dividendInt.divide(greatestCommonDivisor).longValue(), 
            divisorInt.divide(greatestCommonDivisor).longValue(), 
    };
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: user29028183