Numeric

The Numeric registry includes a range of utilities for performing numerical operations and calculations, making it easier to handle numbers and perform math functions in your templates.

You can easily import all the functions from the numeric registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/numeric"

floor

The function returns the largest integer that is less than or equal to the provided number.

Signature

Floor(num any) float64

Must version

❌

{{ 3.7 | floor }} // Output: 3
{{ floor 1.5 }} // Output: 1
{{ floor 123.9999 }} // Output: 123
{{ floor 123.0001 }} // Output: 123

ceil

The function returns the smallest integer that is greater than or equal to the provided number.

Signature

Ceil(num any) float64

Must version

❌

{{ 3.1 | ceil }} // Output: 4
{{ ceil 1.5 }} // Output: 2
{{ ceil 123.9999 }} // Output: 124
{{ ceil 123.0001 }} // Output: 124

round

The function rounds a number to a specified precision, allowing control over the number of decimal places. It also considers an optional rounding threshold to determine whether to round up or down (default to 0.5).

Signature

Round(num any, poww int, roundOpts ...float64) float64

Must version

❌

{{ 3.746, 2, 0.5 | round }} // Output: 3.75
{{ round 3.746 2 }} // Output: 3.75
{{ round 3.746 2 0.5 }} // Output: 3.75
{{ round "123.5555" 3 }} // Output: 123.556
{{ round 123.49999999 0 }} // Output: 123
{{ round 123.2329999 2 .3 }} // Output: 123.23

add / addf

The function performs addition on a slice of values, summing all elements in the slice and returning the total.

Signature

Add(values ...any) any

Must version

❌

{{ add }} // Output: 0
{{ add 1 }} // Output: 1
{{ add 1 2 3 4 5 6 7 8 9 10 }} // Output: 55
{{ add 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.1 }} // Output: 59.6

add1 / add1f

The function performs a unary addition, incrementing the provided value by one.

Signature

Add1(x any) any

Must version

❌

{{ add1 -1 }} // Output: 0
{{ add1f -1.0}} // Output: 0.0
{{ add1 1 }} // Output: 2
{{ add1 1.1 }} // Output: 2.1

sub / subf

The function performs subtraction on a slice of values, starting with the first value and subtracting each subsequent value from it.

Signature

Sub(values ...any) any

Must version

❌

{{ 10, 3, 2 | sub }} // Output: 5

mul

The function multiplies a sequence of values together and returns the result as an int64.

Signature

MulInt(values ...any) int64

Must version

❌

{{ mul 1 1 }} // Output: 1
{{ mul 1.1 1.1 }} // Output: 1
{{ 3 | mul 14 }} // Output: 42

mulf

The function multiplies a sequence of values and returns the result as a float64.

Signature

Mulf(values ...any) any

Must version

❌

{{ mulf 1.1 1.1 }} // Output: 1.2100000000000002

div

The function divides a sequence of values and returns the result as an int64.

Signature

DivInt(values ...any) int64

Must version

❌

{{ div 1 1 }} // Output: 1
{{ div 1.1 1.1 }} // Output: 1

divf

The function divides a sequence of values, starting with the first value, and returns the result as a float64.

Signature

Divf(values ...any) any

Must version

❌

{{ 30.0, 3.0, 2.0 | divf }} // Output: 5.0
{{ 2 | divf 5 4 }} // Output: 0.625

mod

The function returns the remainder of the division of x by y.

Signature

Mod(x any, y any) any

Must version

❌

{{ 10 | mod 4 }} // Output: 2

min

The function returns the minimum value among the provided arguments.

Signature

Min(a any, i ...any) int64

Must version

❌

{{ 5, 3, 8, 2 | min }} // Output: 2

minf

The function returns the minimum value among the provided floating-point arguments.

Signature

Minf(a any, i ...any) float64

Must version

❌

{{ 5.2, 3.8, 8.1, 2.6 | minf }} // Output: 2.6

max

The function returns the maximum value among the provided arguments.

Signature

Max(a any, i ...any) int64

Must version

❌

{{ 5, 3, 8, 2 | max }} // Output: 8

maxf

The function returns the maximum value among the provided floating-point arguments.

Signature

Maxf(a any, i ...any) float64

Must version

❌

{{ 5.2, 3.8, 8.1, 2.6 | maxf }} // Output: 8.1

tt

Last updated

#18: v0.5 loaders with handlers and registries

Change request updated