Round a float to two decimal places

Problem Statement

The company ABC while doing some of its computations related to their supply weights, are getting values with more than two digits after the decimal point.

The company ABC needs a function written in Python that rounds the given value to two digits after the decimal point.

Input

The first line of input contains a float value num.

Output

Print the rounded value of given float value, to two decimal places.

As is

The boilerplate code is already there to read the input from user as an float value into a variable, in the main() function. Also it calls the round_to_2_decimal_places() function, and prints the returned value. Do not edit this code.

To do ✍

absolute_value() has one parameter: num which is an integer value. Write code inside the function body to compute the absolute value of given integer value, and return the absolute value.

Example 1

Test input

3.143562

Test output

3.14

Explanation

The rounding of the given value to two decimal places finds 3 in the third decimal place, which is less than 5. So, it discards the digits from the third place.

Example 2

Test input

1.2986124

Test output

1.3

Explanation

The rounding of the given value to two decimal places finds 8 in the third place. So, it increments the digit before it making the result 1.3.

Useful References

Python Program

# Complete the following function
# Round to last two decimal places
def round_to_2_decimal_places(num):




# Boilerplate code - Do not edit the following
def main():
    num = float(input())
    print(round_to_2_decimal_places(num))

if __name__ == "__main__":
    main()

Test case 1

0
0.0

Test case 2

0.12309
0.12

Test case 3

4.9987
5.0

Test case 4

-5.216
-5.22
# Complete the following function
# Round to last two decimal places
def round_to_2_decimal_places(num):
    rounded_value = round(num, 2)
    return rounded_value

# Boilerplate code - Do not edit the following
def main():
    num = float(input())
    print(round_to_2_decimal_places(num))

if __name__ == "__main__":
    main()
show answer
main.py
⏵︎ Run Tests
Reset
Download
×
Code copied to clipboard successfully 👍