From 86d326a86e5cf6315d2445016b8d5ac7c7c73b80 Mon Sep 17 00:00:00 2001 From: Alan Quillin Date: Fri, 5 May 2017 14:14:13 -0400 Subject: [PATCH 1/2] Added ceil floor and round functions --- functions.go | 3 +++ numeric.go | 30 ++++++++++++++++++++++++++++++ numeric_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/functions.go b/functions.go index e7b30685..b4c39d58 100644 --- a/functions.go +++ b/functions.go @@ -183,6 +183,9 @@ var genericMap = map[string]interface{}{ "biggest": max, "max": max, "min": min, + "ceil": ceil, + "floor": floor, + "round": round, // string slices. Note that we reverse the order b/c that's better // for template processing. diff --git a/numeric.go b/numeric.go index 191e3b97..209c62e5 100644 --- a/numeric.go +++ b/numeric.go @@ -127,3 +127,33 @@ func untilStep(start, stop, step int) []int { } return v } + +func floor(a interface{}) float64 { + aa := toFloat64(a) + return math.Floor(aa) +} + +func ceil(a interface{}) float64 { + aa := toFloat64(a) + return math.Ceil(aa) +} + +func round(a interface{}, p int, r_opt ...float64) float64 { + roundOn := .5 + if len(r_opt) > 0 { + roundOn = r_opt[0] + } + val := toFloat64(a) + places := toFloat64(p) + + var round float64 + pow := math.Pow(10, places) + digit := pow * val + _, div := math.Modf(digit) + if div >= roundOn { + round = math.Ceil(digit) + } else { + round = math.Floor(digit) + } + return round / pow +} \ No newline at end of file diff --git a/numeric_test.go b/numeric_test.go index 97888f49..2f412530 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -2,6 +2,8 @@ package sprig import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestUntil(t *testing.T) { @@ -178,3 +180,26 @@ func TestMul(t *testing.T) { t.Error(err) } } + +func TestCeil(t *testing.T){ + assert.Equal(t, 123.0, ceil(123)) + assert.Equal(t, 123.0, ceil("123")) + assert.Equal(t, 124.0, ceil(123.01)) + assert.Equal(t, 124.0, ceil("123.01")) +} + +func TestFloor(t *testing.T){ + assert.Equal(t, 123.0, floor(123)) + assert.Equal(t, 123.0, floor("123")) + assert.Equal(t, 123.0, floor(123.9999)) + assert.Equal(t, 123.0, floor("123.9999")) +} + +func TestRound(t *testing.T){ + assert.Equal(t, 123.556, round(123.5555, 3)) + assert.Equal(t, 123.556, round("123.55555", 3)) + assert.Equal(t, 124.0, round(123.500001, 0)) + assert.Equal(t, 123.0, round(123.49999999, 0)) + assert.Equal(t, 123.23, round(123.2329999, 2, .3)) + assert.Equal(t, 123.24, round(123.233, 2, .3)) +} From f6e5c4edb5bf19979ba526fbb83d33a09c3ce160 Mon Sep 17 00:00:00 2001 From: Alan Quillin Date: Thu, 25 May 2017 22:59:32 -0400 Subject: [PATCH 2/2] Added documentation for the new math functions: floor, ceil and round --- docs/math.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/math.md b/docs/math.md index d0940fa8..95f2f1e5 100644 --- a/docs/math.md +++ b/docs/math.md @@ -44,3 +44,20 @@ Return the smallest of a series of integers. `min 1 2 3` will return `1`. +## floor + +Returns the greatest float value less than or equal to input value + +`floor 123.9999` will return `123.0` + +## ceil + +Returns the greatest float value greater than or equal to input value + +`ceil 123.001` will return `124.0` + +## round + +Returns a float value with the remainder rounded to the given number to digits after the decimal point. + +`round 123.555555` will return `123.556` \ No newline at end of file