Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-1354

Easy conversion from BigDecimal to BigFraction

    XMLWordPrintableJSON

Details

    • Wish
    • Status: Resolved
    • Major
    • Resolution: Won't Do
    • None
    • 4.0
    • None

    Description

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

          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)));
          }
      

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

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              dobes_vandermeer Dobes Vandermeer
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: