mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
tpl: Add math.Ceil, Floor, and Round
Ceil and Floor are frontends for the stdlib math functions. The Round implementation is essentially the same thing except that the Go stdlib doesn't include a Round implementation in a stable release yet. I've included the Round function slated for Go 1.10. Fixes #3883
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
80c7ea60a0
commit
19c5910485
@@ -34,11 +34,31 @@ func (ns *Namespace) Add(a, b interface{}) (interface{}, error) {
|
||||
return DoArithmetic(a, b, '+')
|
||||
}
|
||||
|
||||
// Ceil returns the least integer value greater than or equal to x.
|
||||
func (ns *Namespace) Ceil(x interface{}) (float64, error) {
|
||||
xf, err := cast.ToFloat64E(x)
|
||||
if err != nil {
|
||||
return 0, errors.New("Ceil operator can't be used with non-float value")
|
||||
}
|
||||
|
||||
return math.Ceil(xf), nil
|
||||
}
|
||||
|
||||
// Div divides two numbers.
|
||||
func (ns *Namespace) Div(a, b interface{}) (interface{}, error) {
|
||||
return DoArithmetic(a, b, '/')
|
||||
}
|
||||
|
||||
// Floor returns the greatest integer value less than or equal to x.
|
||||
func (ns *Namespace) Floor(x interface{}) (float64, error) {
|
||||
xf, err := cast.ToFloat64E(x)
|
||||
if err != nil {
|
||||
return 0, errors.New("Floor operator can't be used with non-float value")
|
||||
}
|
||||
|
||||
return math.Floor(xf), nil
|
||||
}
|
||||
|
||||
// Log returns the natural logarithm of a number.
|
||||
func (ns *Namespace) Log(a interface{}) (float64, error) {
|
||||
af, err := cast.ToFloat64E(a)
|
||||
@@ -92,6 +112,16 @@ func (ns *Namespace) Mul(a, b interface{}) (interface{}, error) {
|
||||
return DoArithmetic(a, b, '*')
|
||||
}
|
||||
|
||||
// Round returns the nearest integer, rounding half away from zero.
|
||||
func (ns *Namespace) Round(x interface{}) (float64, error) {
|
||||
xf, err := cast.ToFloat64E(x)
|
||||
if err != nil {
|
||||
return 0, errors.New("Round operator can't be used with non-float value")
|
||||
}
|
||||
|
||||
return _round(xf), nil
|
||||
}
|
||||
|
||||
// Sub subtracts two numbers.
|
||||
func (ns *Namespace) Sub(a, b interface{}) (interface{}, error) {
|
||||
return DoArithmetic(a, b, '-')
|
||||
|
Reference in New Issue
Block a user