DEV Community

Cover image for Round up decimals with specifying decimal digit length in Python
Tomoyuki KOYAMA
Tomoyuki KOYAMA

Posted on

2

Round up decimals with specifying decimal digit length in Python

The module math.ceil() enables to round up decimals in Python. This module only covers a type from float to int. This post shows tips that round up decimals by specifying decimal digit length.

def my_ceil(x: Union[float, int], exponential: int = 2) -> float:
    import math
    _expo = 10 ** exponential
    return math.ceil(x * _expo) / _expo
Enter fullscreen mode Exit fullscreen mode

Top comments (0)