Numeric functions

These functions are used to manipulate numeric values. Most of them take a single numeric argument and produce a numeric result.

ceil(x)

Returns the value x rounded up towards positive infinity. In other words, this returns the smallest integer value greater than or equal to x.

title: ceil
---
{ ceil(0)     }  // 0
{ ceil(0.3)   }  // 1
{ ceil(5)     }  // 5
{ ceil(5.001) }  // 6
{ ceil(5.999) }  // 6
{ ceil(-2.07) }  // -2
===

See also

dec(x)

Returns the value x reduced towards the previous integer. Thus, if x is already an integer this returns x - 1, but if x is not an integer then this returns floor(x).

title: dec
---
{ dec(0)     }  // -1
{ dec(0.3)   }  // 0
{ dec(5.0)   }  // 4
{ dec(5.001) }  // 5
{ dec(5.999) }  // 5
{ dec(-2.07) }  // -3
===

See also

decimal(x)

Returns a fractional part of x.

If x is positive, then the returned value will be between 0 (inclusive) and 1 (exclusive). If x is negative, then the returned value will be between 0 and -1. In all cases it should hold that x == int(x) + decimal(x).

title: decimal
---
{ decimal(0)     }  // 0
{ decimal(0.3)   }  // 0.3
{ decimal(5.0)   }  // 0
{ decimal(5.001) }  // 0.001
{ decimal(5.999) }  // 0.999
{ decimal(-2.07) }  // -0.07
===

See also

floor(x)

Returns the value x rounded down towards negative infinity. In other words, this returns the largest integer value less than or equal to x.

title: floor
---
{ floor(0)     }  // 0
{ floor(0.3)   }  // 0
{ floor(5)     }  // 5
{ floor(5.001) }  // 5
{ floor(5.999) }  // 5
{ floor(-2.07) }  // -3
===

See also

inc(x)

Returns the value x increased towards the next integer. Thus, if x is already an integer this returns x + 1, but if x is not an integer then this returns ceil(x).

title: inc
---
{ inc(0)     }  // 1
{ inc(0.3)   }  // 1
{ inc(5.0)   }  // 6
{ inc(5.001) }  // 6
{ inc(5.999) }  // 6
{ inc(-2.07) }  // -2
===

See also

int(x)

Truncates the fractional part of x, rounding it towards zero, and returns just the integer part of the argument x.

title: int
---
{ int(0)     }  // 0
{ int(0.3)   }  // 0
{ int(5.0)   }  // 5
{ int(5.001) }  // 5
{ int(5.999) }  // 5
{ int(-2.07) }  // -2
===

See also

round(x)

Rounds the value x towards a nearest integer.

The values that end with .5 are rounded up if x is positive, and down if x is negative.

title: round
---
{ round(0)     }  // 0
{ round(0.3)   }  // 0
{ round(5.0)   }  // 5
{ round(5.001) }  // 5
{ round(5.5)   }  // 6
{ round(5.999) }  // 6
{ round(-2.07) }  // -2
{ round(-2.5) }   // -3
===

round_places(x, n)

Rounds the value x to n decimal places.

The value x can be either positive, negative, or zero, but it must be an integer. Rounding to 0 decimal places is equivalent to the regular round(x) function. If n is positive, then the function will attempt to keep that many digits after the decimal point in x. If n is negative, then round_places() will round x to nearest tens, hundreds, thousands, etc:

title: round_places
---
{ round_places(0, 1)     }  // 0
{ round_places(0.3, 1)   }  // 0.3
{ round_places(5.001, 1) }  // 5.0
{ round_places(5.001, 2) }  // 5.0
{ round_places(5.001, 3) }  // 5.001
{ round_places(5.5, 1)   }  // 5.5
{ round_places(5.999, 1) }  // 6.0
{ round_places(-2.07, 1) }  // -2.1
{ round_places(13, -1)   }  // 10
{ round_places(252, -2)  }  // 200
===

See also