Sunday, April 3, 2011

Is there a CEIL version of MySQL's DIV operator?

With MySQL

a DIV b

is much faster than

FLOOR(a / b).

But I need to round up so I'm using,

CEIL(a / b)

It seems strange that there wouldn't be a ceiling version of DIV, but I can't find it. Is there anything undocumented hidden away somewhere? Or any other non floating point way to do this?

From stackoverflow
  • For a > 0 you can write

    (a - 1) div b + 1
    
    finnw : Be careful - what happens if a is 0?
    Gleb : Thanks, actually (a + b - 1) DIV b would be better I guess.
  • Alternative:

    (a + b - 1) DIV b

0 comments:

Post a Comment