Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:33:59+00:00 2026-06-05T15:33:59+00:00

What I need to do is use integer arithmetic to convert fractions into floating

  • 0

What I need to do is use integer arithmetic to convert fractions into floating point numbers. The number of decimal places needed is specified as a variable DECIMALS. Each fraction is contained in a tuple of integers, for example (1, 3). The first item is the numerator and the second is the denominator. The tuples are contained in a list called fractions.

This is my code so far:

fractions = [(1,7), (2,3), (22,7), (7001,7), (9,3), (611951,611953), (1,11), (1,7689585)]

DECIMALS = 10**40 # 40 decimal places

for fraction in fractions:  
     x =  DECIMALS*fraction[0]/fraction[1]
     print x

When i run the code this is what i get:

1428571428571428571428571428571428571428
6666666666666666666666666666666666666666
31428571428571428571428571428571428571428   
10001428571428571428571428571428571428571428
30000000000000000000000000000000000000000
9999967317751526669531810449495304377950
909090909090909090909090909090909090909
1300460297922449651053990559958697

The problem is that I need to format this into the correct decimal format. What I tried was

print "0.%.40d" % (x)

But of course this will only help me with the first 2 decimals; the rest will be wrong. I thought about dividing it up so I can calculate the fractions indivdually to make them easier to format, but I have no idea how to do this. The catch is that all the fractions need to be processed by the same code. I also want it to be rounded properly but thats not a big deal right now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:34:01+00:00Added an answer on June 5, 2026 at 3:34 pm

    If it strictly formatting, as implied by your question, you could, of course, do this with integers and strings alone:

    def intF(n, d, l=40):
        s=str(n*10**l / d)
        if len(s) < l:
            return '0.{:0>{width}}'.format(s,width=l)
        if len(s) > l:
            return s[0:len(s)-l]+'.'+s[len(s)-l:] 
    
        return '0.'+s
    
    
    for f in [(1,7), (2,3), (22,7), (7001,7), (9,3), 
              (611951,611953), (1,11),(1,7689585)]:
        print intF(*f)
        print float(f[0]) / f[1]
        print 
    

    Output:

    0.1428571428571428571428571428571428571428
    0.142857142857
    
    0.6666666666666666666666666666666666666666
    0.666666666667
    
    3.1428571428571428571428571428571428571428
    3.14285714286
    
    1000.1428571428571428571428571428571428571428
    1000.14285714
    
    3.0000000000000000000000000000000000000000
    3.0
    
    0.9999967317751526669531810449495304377950
    0.999996731775
    
    0.0909090909090909090909090909090909090909
    0.0909090909091
    
    0.0000001300460297922449651053990559958697
    1.30046029792e-07
    

    The last digit will not correctly round and it will not handle edge cases (‘inf’, ‘NaN’, division by zero, etc) correctly.

    Works in a pinch I suppose.

    Why not use the batteries included though, like Decimal or Fraction?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do some floating point arithmetic in Java as shown in the
I need to use Integer Linear Programming API/Tool for my application. Though my application
I need to use a binary data in a transformation as an integer and
So, I need use this event so I can navigate trought blog posts. I
Need to use own imaged markers instead built-in pins. I have several questions. 1.
i need to use the animate property for a less than usual activity. i
I need to use scp update some directory at another server. It is similar
I need to use some classes inside the app_code folder of a website project
I need to use something like get_or_create() but the problem is that I have
I need to use Twitter api from iPhone 3.0 (SDK 3.0). can i use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.