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 8085795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:14:32+00:00 2026-06-05T18:14:32+00:00

I need help understanding whats going on in the function, especially the return statements.

  • 0

I need help understanding whats going on in the function, especially the return statements. I know what the return statements do but not how they do it. I know that they format the string but I just don’t understand how its being done. It would help if you guys take it step by step.

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
  • 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-05T18:14:33+00:00Added an answer on June 5, 2026 at 6:14 pm

    Here’s a line-by-line breakdown:

    def intF(n, d, l=40):
    

    Pretty obvious. n is a number, d is another number (the divisor) and l is the number of digits to print after the decimal point.

        s=str(n*10**l / d)
    

    This does something a bit unusual. Rather than relying on floating point arithmetic, this multiplies n by 10 ** l, i.e. by a 1 followed by l digits. That way the final result won’t have any floating point error — assuming d is always an integer. (But of course any remaining digits get truncated. Also, replace / with // in Python 3 to get the same behavior.)

    At this point, s will be a string representation of a whole number — again assuming d is an integer — but it will have the same digits as the result of float(n) / d. So now we just have to insert the decimal point in the right place.

        if len(s) < l: 
            return '0.{:0>{width}}'.format(s,width=l) 
    

    If the length of s is less than l, then we need to pad it and prepend a 0.. That’s what this does. The {:0>{width}} field says to create a zero-padded field of width width, and insert a value into it on the right (>) side. Then s is passed in via format, and we have our result.

        if len(s) > l: 
            return s[0:len(s)-l]+'.'+s[len(s)-l:]
    

    If the length of a is greater than l, then we need to insert the decimal point in the correct spot. That’s what this does. It removes the trailing l digits from s, appends a ., and then appends the remaining l digits.

        return '0.'+s
    

    The final possibility is that s is exactly l digits long. In that case, we don’t need to do any padding; we can just prepend a 0 and a decimal point.

    As a final note: if you pass anything but integers to this function, it will not work as expected. Consider this:

    >>> intF(10, 10.1, 10)
    '990.0990099.01'
    

    Or this:

    >>> intF(10.1, 10, 10)
    '101.00000000.0'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to c# and need help understanding what going on in the
Kind of need help understanding what this code actually outputs. Does it out put
I need some help in understanding what is happening here .I am getting a
I need help understanding the proper sequence of filtering data when sending user inputted
I need help understanding how to demonize a process in Go. package main import
Need some help understanding the difference between the success and ajaxSuccess events documented here
I need some help understanding the advantages of using CountDownLatch over traditional wait-notify. I
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need some help in understanding when and why to use Remote Service instead
I have been trying out Cassandra and need some help in understanding a few

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.