Question: Problem 4 . 2 ( 1 0 pts ) : Given that floating - point numbers are an approximation anyway, we should devise a way

Problem 4.2(10 pts): Given that floating-point numbers are an approximation anyway, we should devise a way
to round these numbers to two decimal places, as that will be plenty accurate for our purposes. Define and
implement a function named exactly round_to_two, that takes a decimal number and returns that number
rounded to only two decimal places. Your function should round using the Half Round-Up method, so a number
such as 4.2568 would round to 4.0 for zero decimal places, 4.3 for one, 4.26 for two, and so on.
To receive any credit for this problem, you are not allowed to use the Python built-in round() function!
Hint:
The modulus operator and the floor division operator are handy tools when working with values that are better split into
their remainder and whole divisor portions. While it might be mathematically silly to divide or modulate by one when using
integers, this is a very helpful technique when working with floats!
123.456//1.0 evaluates to 123.0
123.456%1.0 evaluates to 0.456
In Module 2 lectures, we presented the technique to round for zero decimal places. If we want to use the same technique
to round to one decimal place, we need to first shift the decimal point one place to the right (value *10), apply the
technique to round, and shift the decimal point back to the left (value = value /10) to get the desired result. This
approach can be extended to round to two decimal places!
Examples:
>>> round_to_two(85.6397)
85.64
>>> round_to_two(98.75)
98.75
>>> round_to_two(8.1)
8.1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!