From db72567ed07dd958157ba0aee5c157e688deadd6 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 21 May 2017 18:30:07 +0200 Subject: Expose n --- round.go | 8 ++------ round_test.go | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/round.go b/round.go index 89725f2..d1c625e 100644 --- a/round.go +++ b/round.go @@ -6,13 +6,9 @@ import "math" // q = \sgn(y) \left\lfloor \left| y \right| + 0.5 \right\rfloor // = -\sgn(y) \left\lceil -\left| y \right| - 0.5 \right\rceil -func round(v float64, places int) float64 { +// Round a float value to n decimal places +func Round(v float64, places int) float64 { scale := math.Pow(10, float64(places)) abs := math.Abs(v*scale) + 0.5 return math.Copysign(math.Floor(abs)/scale, v) } - -// Round a float value to 2 decimal places -func Round(v float64) float64 { - return round(v, 2) -} diff --git a/round_test.go b/round_test.go index adabd96..13a9051 100644 --- a/round_test.go +++ b/round_test.go @@ -24,7 +24,7 @@ func TestRound(t *testing.T) { } for _, tc := range testCases { t.Run(fmt.Sprint(tc.in), func(t *testing.T) { - if r := Round(tc.in); r != tc.out { + if r := Round(tc.in, 2); r != tc.out { t.Errorf("got %v, want %v", r, tc.out) } }) @@ -33,6 +33,6 @@ func TestRound(t *testing.T) { func BenchmarkRound(b *testing.B) { for i := 0; i < b.N; i++ { - Round(0.333) + Round(0.333, 2) } } -- cgit v1.2.3