You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Dobes Vandermeer (JIRA)" <ji...@apache.org> on 2016/03/31 19:38:25 UTC

[jira] [Created] (MATH-1354) Easy conversion from BigDecimal to BigFraction

Dobes Vandermeer created MATH-1354:
--------------------------------------

             Summary: Easy conversion from BigDecimal to BigFraction
                 Key: MATH-1354
                 URL: https://issues.apache.org/jira/browse/MATH-1354
             Project: Commons Math
          Issue Type: Wish
            Reporter: Dobes Vandermeer


Users of BigFraction might also be working with BigDecimal.  Here's a simple way to convert BigDecimal to BigFraction:

{code}
    public static BigFraction bigDecimalToBigFraction(BigDecimal bd) {
        int scale = bd.scale();

        // If scale >= 0 then the value is bd.unscaledValue() / 10^scale
        if(scale >= 0)
            return new BigFraction(bd.unscaledValue(), BigInteger.TEN.pow(scale));
        // If scale < 0 then the value is bd.unscaledValue() * 10^-scale
        return new BigFraction(bd.unscaledValue().multiply(BigInteger.TEN.pow(-scale)));
    }
{code}

It might be nice to have this incorporated into the BigFraction class as a constructor.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)