Answer to Question #174819 in Python for Hari Chandana T

Question #174819

Please follow the introduction to write Python code for rounding withour use round function .screenshot and explanation request!Thanks in advance!

1. first need to check if the number is positive or negative and save

2. Get rid of any numbers you don't need.

4. Get the last digit.

5. if >5 add 1 , If less than 5, leave it , If equal to 5, the last number should be even.

6. Convert it back to decimal

Write a function round(number[, ndigits]) that models the behavior of the built-in function.

Input: a float number, and an optional argument, a non-negative integer ndigits. The default parameter

value for ndigits is 0.

Output: if ndigits is 0, an integer, else a float, rounded to ndigits after the decimal point. If the first

dropped digit is greater than 5, round up; if the first dropped digit is less than 5, round down; else round

in such a way that the remaining digit in the smallest position is even.


1
Expert's answer
2021-03-24T12:46:21-0400
def roundof():
    number =float(input('enter number'))
    st=repr(number)
    sign_digit,frac_digit=st.split('.')
    if(int(frac_digit)%10>5):
        stri=frac_digit
        strin=stri[0:len(frac_digit)-1]
        signum=int(strin)+1
        value=str(signum)
        joinString=sign_digit+'.'+value
        print('required significant number is:',joinString)
    else:
        stri = frac_digit
        strin = stri[0:len(frac_digit) - 1]
        signum = int(strin)
        value = str(signum)
        joinString = sign_digit + '.' + value
        print(joinString)




roundof()

Output:





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS