Convert Ratio To Decimal

May 15, 2015

Here’s our solution, which was indeed written within fifteen minutes:

(define (decimal num denom digits)
  (let loop ((num (* (modulo num denom) 10))
             (out (cons #\. (reverse (string->list
               (number->string (quotient num denom))))))
             (digits digits))
    (if (zero? digits)
        (list->string (reverse out))
        (loop (* (modulo num denom) 10)
              (cons (integer->char (+ (quotient num denom) 48)) out)
              (- digits 1)))))

This is nothing more than the grade-school long-division algorithm, generating the output one digit at a time; out is initialized to the part of the quotient to the left of the decimal point, in reverse:

> (decimal 3227 557 30)
"5.793536804308797127468581687612"

You can run the program at http://ideone.com/hiNpKQ.

Advertisement

Pages: 1 2

5 Responses to “Convert Ratio To Decimal”

  1. Paul said

    In Python.

    from decimal import Decimal, getcontext
    
    def round_frac1(num, den, digits):
        getcontext().prec = digits
        return Decimal(num) / Decimal(den)
        
    def round_frac2(num, den, digits):
        res = ["" if num >= 0 else "-"]
        div, mod = divmod(abs(num), den)
        res += [str(div), "."]
        for d in range(digits):
            if mod == 0:
                break
            div, mod = divmod(mod * 10, den)
            res.append(str(div))
        return "".join(res)
    
    print(round_frac1(3227, 557, 31)) # 5.793536804308797127468581687612
    print(round_frac2(3227, 557, 30)) # 5.793536804308797127468581687612
    
  2. Globules said

    A quick ‘n dirty Haskell version.

    -- The result of a/b with n digits after the decimal point.
    divide n a b = let (q, r) = a `quotRem` b
                   in show q ++ "." ++ concatMap show (take n (r `fr` b))
      where a `fr` b = let (q, r) = (10*a) `quotRem` b in q : (r `fr` b)
    

    A couple of examples in ghci:

    λ> putStrLn $ divide 16 13 17
    0.7647058823529411
    λ> 13/17
    0.7647058823529411
    λ> putStrLn $ divide 30 3227 557
    5.793536804308797127468581687612
    λ> 
    
  3. Vaibhav Jain said

    In C++

    #include

    using namespace std;

    int main()
    {
    int den,num,dig,temp1,temp2;
    cout <> den;
    cout <> num;
    cout <> dig;
    cout << num/den << "."; // Whole number part of decimal followed by decimal point
    temp1=num;
    for (int i=0;i<30;i++)
    {
    temp2=(temp1%den)*10;
    cout << temp2/den;
    temp1=temp2%den;
    }
    return 0;
    }

  4. Christophe said

    Clojure
    I admit, it took me longer than 15 minutes..

    (defn long-division
      [nom denom decimals]
      (loop [result   (str (quot nom denom) ".")
             nom          (* (mod nom denom) 10)
             decimals                   decimals]
        (if (>  decimals 0 )
          (recur (str result (quot nom denom))
                 (* (mod nom denom) 10)
                 (dec decimals))
          result)))
    
    
    
    (println  (long-division 3227 557 30))
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: