summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-05-21 18:30:07 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-05-21 18:30:07 +0200
commitdb72567ed07dd958157ba0aee5c157e688deadd6 (patch)
tree7babd3cdc84e9902e3628c3a042bdff7f8e4c7f7
parentd6e30494ed753c634a702acb2174a0550bbb6342 (diff)
Expose n
-rw-r--r--round.go8
-rw-r--r--round_test.go4
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)
}
}