How do you roundup a number?

How do you roundup a number?

Hey Prynka,

In Python, you can round up a number using the math.ceil() function from the math module. Here’s an example:

import math

number = 4.3 rounded_up = math.ceil(number)

print(rounded_up) # Output: 5

The math.ceil() function returns the smallest integer greater than or equal to the given number, effectively rounding up the number to the nearest whole number.

Hello Prynka,

Using the decimal module: The decimal module in Python provides a way to work with decimal numbers with arbitrary precision. In this example, we first create a Decimal object with the value ‘4.3’. Then, we use the to_integral_value() method with ROUND_CEILING rounding mode to round the number up to the nearest integer. The ROUND_CEILING mode rounds towards positive infinity.

import decimal

number = decimal.Decimal(‘4.3’) rounded_up = number.to_integral_value(rounding=decimal.ROUND_CEILING)

print(rounded_up) # Output: Decimal(‘5’)

Hello Prynka,

Using simple arithmetic: In this approach, we use the floor division operator // to divide the number by 1, effectively truncating the decimal part and keeping only the integer part. However, to round up, we use a trick: we negate the number, apply the floor division, and negate the result again. This works because floor division always rounds towards negative infinity, so by negating the number first, we effectively round towards positive infinity.

number = 4.3 rounded_up = -int(-number // 1)

print(rounded_up) # Output: 5