Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel-like ceiling function in python?

I know about math.ceil and numpy.ceil, but both of them lack of significance parameter. For example in Excel:

=Ceiling(210.63, 0.05) -> 210.65

numpy.ceil and math.ceil in other hand:

numpy.ceil(210.63) -> 211.0

math.ceil(210.63) -> 211.0

So, I wonder, Is there some similar to Excel's solution already?

like image 445
Vladimiroff Avatar asked Oct 06 '11 08:10

Vladimiroff


People also ask

Is there a ceiling function in Python?

The ceil() Function:The method ceil(x) in Python returns a ceiling value of x i.e., the smallest integer greater than or equal to x. Syntax: import math math.ceil(x) Parameter: x:This is a numeric expression. Returns: Smallest integer not less than x.

Does Python have ceiling division?

Understanding Python Ceiling()The ceil() function is part of the built-in math module and takes a single argument, a number. The function returns a single number, an integer, that is the smallest integer greater than or equal to the number passed into the argument.

Is there a ceiling function in Excel?

The Excel CEILING function[1] is categorized under Math and Trigonometry functions. The function will return a number that is rounded up to a supplied number that is away from zero to the nearest multiple of a given number. MS Excel 2016 handles both positive and negative arguments.


1 Answers

I don't know of any python function to do so, but you can easily code one :

import math

def ceil(x, s):
    return s * math.ceil(float(x)/s)

The conversion to float is necessary in python 2 to avoid the integer division if both arguments are integers. You can also use from __future__ import division. This is not needed with python 3.

like image 112
madjar Avatar answered Sep 22 '22 12:09

madjar