Ceil

2 snippets across 2 stacks — JavaScript, Python

JSJavaScript

Rounding

JS · Numbers & Math
syntax
Math.round(n) | Math.floor(n) | Math.ceil(n) | Math.trunc(n)
example
const price = 19.872;
console.log(Math.round(price));  // 20
console.log(Math.floor(price));  // 19
console.log(Math.ceil(price));   // 20
console.log(Math.trunc(price));  // 19
console.log(Math.trunc(-3.7));   // -3 (not -4)

Note trunc() simply removes decimals (towards zero). floor() rounds towards negative infinity. They differ for negative numbers: floor(-3.2) = -4 but trunc(-3.2) = -3.

PYPython

math Module Essentials

PY · Numbers
syntax
import math
example
import math
print(math.ceil(4.2))
print(math.floor(4.8))
print(math.sqrt(144))
print(math.log(100, 10))
print(math.pi)
output
5
4
12.0
2.0
3.141592653589793

Note math functions work on ints and floats but not on complex numbers. Use cmath for complex math operations.