Question

Using only string operations and basic math operations (you can't use specific method that does math...

Using only string operations and basic math operations (you can't use specific method that does math operations), write a Python function to truncate a given floating point number to to given specific number of decimal places. Here you have to validate the input number for a floating pint number and if the user input an integer simply return that number itself as there is no decimal places to truncate. For example, for the number 9.9999 to truncate to 2 decimal places you would return 9.99.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def change(f, n):

f = str(f)

index = f.find('.')

result = f[:index+1+n]

return float(result)

print(change(9.9999, 2))

# Output: 9.99

Add a comment
Know the answer?
Add Answer to:
Using only string operations and basic math operations (you can't use specific method that does math...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT