Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ Multiply with `mul`. Accepts two or more inputs.
mul 1 2 3
```

## abs

Return the absolute value of an integer.

This will return `3`:

```
abs -3
```

## max

Return the largest of a series of integers:
Expand Down
10 changes: 10 additions & 0 deletions docs/mathf.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ This will return `1.5`:
```
minf 1.5 2 3
```

## absf

Return the absolute value of a float.

This will return `3.14`:

```
absf -3.14
```
2 changes: 2 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ var genericMap = map[string]interface{}{
"min": min,
"maxf": maxf,
"minf": minf,
"abs": abs,
"absf": absf,
"ceil": ceil,
"floor": floor,
"round": round,
Expand Down
15 changes: 14 additions & 1 deletion numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strconv"
"strings"

"github.com/spf13/cast"
"github.com/shopspring/decimal"
"github.com/spf13/cast"
)

// toFloat64 converts 64-bit floats
Expand Down Expand Up @@ -64,6 +64,19 @@ func minf(a interface{}, i ...interface{}) float64 {
return aa
}

func abs(a interface{}) int64 {
aa := toInt64(a)
if aa < 0 {
return -aa
}
return aa
}

func absf(a interface{}) float64 {
aa := toFloat64(a)
return math.Abs(aa)
}

func until(count int) []int {
step := 1
if count < 0 {
Expand Down
14 changes: 14 additions & 0 deletions numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ func TestSubf(t *testing.T) {
}
}

func TestAbs(t *testing.T) {
tpl := `{{ abs -3 }}`
if err := runt(tpl, `3`); err != nil {
t.Error(err)
}
}

func TestAbsf(t *testing.T) {
tpl := `{{ absf -3.4 }}`
if err := runt(tpl, `3.4`); err != nil {
t.Error(err)
}
}

func TestCeil(t *testing.T) {
assert.Equal(t, 123.0, ceil(123))
assert.Equal(t, 123.0, ceil("123"))
Expand Down