diff --git a/pkg/tplx/fns.go b/pkg/tplx/fns.go index 68950f64659a7d63e9a072e792e3ff94f49c978b..1cda2d5fe7e1c2fea064b96be93bfbd71206f60b 100644 --- a/pkg/tplx/fns.go +++ b/pkg/tplx/fns.go @@ -352,3 +352,13 @@ func Divide(a, b interface{}) (interface{}, error) { return nil, fmt.Errorf("divide: unknown type for %q (%T)", av, a) } } + +func FormatDecimal(s string, n int) string { + num, err := strconv.ParseFloat(s, 64) + if err != nil { + return s + } + + format := fmt.Sprintf("%%.%df", n) + return fmt.Sprintf(format, num) +}