Write a Python function to round up a number to specified digits


This is a Python program that demonstrates how to round a floating-point number to a specified number of decimal places using the math module. The program first initializes a floating-point number x to the value of 88.24573. The print() function is then used to display the original value of x.

A for loop is used to perform the rounding operation five times, with the roundup() function called each time. The roundup() function takes two arguments - a floating-point number to be rounded and an integer specifying the number of decimal places to round to.

The roundup() function uses the math.ceil() function to round the input number up to the next integer, and then divides the result by 10**-digits, where digits is the number of decimal places specified. The result is then rounded to the nearest integer using the built-in round() function. The final result is returned from the function.

The roundup() function is called with the original number x and an integer ranging from 0 to 4, which specifies the number of decimal places to round to. The rounded result is then printed to the console using the print() function.

Source Code

import math
x = 88.24573
print("Original  Number: ",x)
for i in range(5):
	def roundup(a, digits=0):
		n = 10**-digits
		return round(math.ceil(a / n) * n, digits)
	print(roundup(x, i))

Output

Original  Number:  88.24573
89
88.3
88.25
88.246
88.2458

Example Programs