Skip to content

Commit

Permalink
api: move formatFloat function
Browse files Browse the repository at this point in the history
`helpers.FormatFloat` function is only used in `api`.  Moving it and
marking it as private.  We can re-export it if we find value later.
  • Loading branch information
Mahmood Ali committed Jan 18, 2019
1 parent 41c3250 commit b1293a8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 65 deletions.
6 changes: 2 additions & 4 deletions api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"sort"
"strconv"
"time"

"github.com/hashicorp/nomad/helper"
)

// Nodes is used to query node-related API endpoints
Expand Down Expand Up @@ -661,9 +659,9 @@ func (v *StatValue) String() string {
case v.StringVal != nil:
return *v.StringVal
case v.FloatNumeratorVal != nil:
str := helper.FormatFloat(*v.FloatNumeratorVal, 3)
str := formatFloat(*v.FloatNumeratorVal, 3)
if v.FloatDenominatorVal != nil {
str += " / " + helper.FormatFloat(*v.FloatDenominatorVal, 3)
str += " / " + formatFloat(*v.FloatDenominatorVal, 3)
}

if v.Unit != "" {
Expand Down
4 changes: 1 addition & 3 deletions api/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package api

import (
"strconv"

"github.com/hashicorp/nomad/helper"
)

// Resources encapsulates the required resources of
Expand Down Expand Up @@ -169,7 +167,7 @@ type Attribute struct {
func (a Attribute) String() string {
switch {
case a.FloatVal != nil:
str := helper.FormatFloat(*a.FloatVal, 3)
str := formatFloat(*a.FloatVal, 3)
if a.Unit != "" {
str += " " + a.Unit
}
Expand Down
27 changes: 26 additions & 1 deletion api/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

import "time"
import (
"strconv"
"strings"
"time"
)

// boolToPtr returns the pointer to a boolean
func boolToPtr(b bool) *bool {
Expand All @@ -26,3 +30,24 @@ func stringToPtr(str string) *string {
func timeToPtr(t time.Duration) *time.Duration {
return &t
}

// formatFloat converts the floating-point number f to a string,
// after rounding it to the passed unit.
//
// Uses 'f' format (-ddd.dddddd, no exponent), and uses at most
// maxPrec digits after the decimal point.
func formatFloat(f float64, maxPrec int) string {
v := strconv.FormatFloat(f, 'f', -1, 64)

idx := strings.LastIndex(v, ".")
if idx == -1 {
return v
}

sublen := idx + maxPrec + 1
if sublen > len(v) {
sublen = len(v)
}

return v[:sublen]
}
39 changes: 39 additions & 0 deletions api/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package api

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestFormatRoundedFloat(t *testing.T) {
cases := []struct {
input float64
expected string
}{
{
1323,
"1323",
},
{
10.321,
"10.321",
},
{
100000.31324324,
"100000.313",
},
{
100000.3,
"100000.3",
},
{
0.7654321,
"0.765",
},
}

for _, c := range cases {
require.Equal(t, c.expected, formatFloat(c.input, 3))
}
}
23 changes: 0 additions & 23 deletions helper/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"crypto/sha512"
"fmt"
"regexp"
"strconv"
"strings"
"time"

multierror "github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -359,24 +357,3 @@ func CheckHCLKeys(node ast.Node, valid []string) error {

return result
}

// FormatFloat converts the floating-point number f to a string,
// after rounding it to the passed unit.
//
// Uses 'f' format (-ddd.dddddd, no exponent), and uses at most
// maxPrec digits after the decimal point.
func FormatFloat(f float64, maxPrec int) string {
v := strconv.FormatFloat(f, 'f', -1, 64)

idx := strings.LastIndex(v, ".")
if idx == -1 {
return v
}

sublen := idx + maxPrec + 1
if sublen > len(v) {
sublen = len(v)
}

return v[:sublen]
}
34 changes: 0 additions & 34 deletions helper/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"reflect"
"sort"
"testing"

"github.com/stretchr/testify/require"
)

func TestSliceStringIsSubset(t *testing.T) {
Expand Down Expand Up @@ -89,35 +87,3 @@ func BenchmarkCleanEnvVar(b *testing.B) {
CleanEnvVar(in, replacement)
}
}

func TestFormatRoundedFloat(t *testing.T) {
cases := []struct {
input float64
expected string
}{
{
1323,
"1323",
},
{
10.321,
"10.321",
},
{
100000.31324324,
"100000.313",
},
{
100000.3,
"100000.3",
},
{
0.7654321,
"0.765",
},
}

for _, c := range cases {
require.Equal(t, c.expected, FormatFloat(c.input, 3))
}
}

0 comments on commit b1293a8

Please sign in to comment.