Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd23bd8

Browse files
committedJan 9, 2018
modify
1 parent 98c55f2 commit dd23bd8

File tree

3 files changed

+185
-79
lines changed

3 files changed

+185
-79
lines changed
 

‎pmath/round.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package pmath
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"strconv"
7+
)
8+
9+
func Round(f float64, n int) float64 {
10+
n10 := math.Pow10(n)
11+
return math.Trunc((f+0.5/n10)*n10) / n10
12+
}
13+
14+
func Round2(f float64, n int) float64 {
15+
floatStr := fmt.Sprintf("%."+strconv.Itoa(n)+"f", f)
16+
inst, _ := strconv.ParseFloat(floatStr, 64)
17+
return inst
18+
}

‎pmath/round_test.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package pmath_test
2+
3+
import (
4+
"github.com/penguinn/utils/pmath"
5+
"testing"
6+
)
7+
8+
func TestRound(t *testing.T) {
9+
type args struct {
10+
f float64
11+
n int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want float64
17+
}{
18+
{name: "测试1",
19+
args: args{
20+
f: 54.48341896,
21+
n: 2,
22+
},
23+
want: 54.48,
24+
},
25+
{name: "测试2",
26+
args: args{
27+
f: 54.48841896,
28+
n: 2,
29+
},
30+
want: 54.49,
31+
},
32+
{name: "测试3",
33+
args: args{
34+
f: 54.47841896,
35+
n: 2,
36+
},
37+
want: 54.48,
38+
},
39+
}
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
if got := pmath.Round(tt.args.f, tt.args.n); got != tt.want {
43+
t.Errorf("Round() = %v, want %v", got, tt.want)
44+
}
45+
})
46+
}
47+
}
48+
49+
func TestRound2(t *testing.T) {
50+
type args struct {
51+
f float64
52+
n int
53+
}
54+
tests := []struct {
55+
name string
56+
args args
57+
want float64
58+
}{
59+
{name: "测试1",
60+
args: args{
61+
f: 54.48341896,
62+
n: 2,
63+
},
64+
want: 54.48,
65+
},
66+
{name: "测试2",
67+
args: args{
68+
f: 54.48841896,
69+
n: 2,
70+
},
71+
want: 54.49,
72+
},
73+
{name: "测试3",
74+
args: args{
75+
f: 54.47841896,
76+
n: 2,
77+
},
78+
want: 54.48,
79+
},
80+
}
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
if got := pmath.Round2(tt.args.f, tt.args.n); got != tt.want {
84+
t.Errorf("Round2() = %v, want %v", got, tt.want)
85+
}
86+
})
87+
}
88+
}

‎set_util.go

+79-79
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
1-
package utils
2-
3-
import (
4-
"sync"
5-
)
6-
7-
type Set struct {
8-
mapInst map[interface{}]bool
9-
sync.RWMutex
10-
}
11-
12-
func New() *Set {
13-
return &Set{
14-
mapInst: map[interface{}]bool{},
15-
}
16-
}
17-
18-
func (p *Set) Add(item interface{}) {
19-
p.Lock()
20-
defer p.Unlock()
21-
p.mapInst[item] = true
22-
}
23-
24-
func (p *Set) Remove(item interface{}) {
25-
p.Lock()
26-
defer p.Unlock()
27-
delete(p.mapInst, item)
28-
}
29-
30-
func (p *Set) PopOne() (item interface{}, ok bool) {
31-
p.Lock()
32-
defer p.Unlock()
33-
if len(p.mapInst) > 0 {
34-
for k := range p.mapInst {
35-
item = k
36-
delete(p.mapInst, k)
37-
break
38-
}
39-
ok = true
40-
} else {
41-
ok = false
42-
}
43-
return
44-
}
45-
46-
func (p *Set) Has(item interface{}) bool {
47-
p.RLock()
48-
defer p.RUnlock()
49-
_, ok := p.mapInst[item]
50-
return ok
51-
}
52-
53-
func (p *Set) Len() int {
54-
return int(len(p.mapInst))
55-
}
56-
57-
func (p *Set) Clear() {
58-
p.Lock()
59-
defer p.Unlock()
60-
p.mapInst = map[interface{}]bool{}
61-
}
62-
63-
func (p *Set) IsEmpty() bool {
64-
if p.Len() == 0 {
65-
return true
66-
}
67-
return false
68-
}
69-
70-
func (p *Set) List() []interface{} {
71-
p.RLock()
72-
defer p.RUnlock()
73-
list := []interface{}{}
74-
75-
for item := range p.mapInst {
76-
list = append(list, item)
77-
}
78-
return list
79-
}
1+
package utils
2+
3+
import (
4+
"sync"
5+
)
6+
7+
type Set struct {
8+
mapInst map[interface{}]bool
9+
sync.RWMutex
10+
}
11+
12+
func New() *Set {
13+
return &Set{
14+
mapInst: map[interface{}]bool{},
15+
}
16+
}
17+
18+
func (p *Set) Add(item interface{}) {
19+
p.Lock()
20+
defer p.Unlock()
21+
p.mapInst[item] = true
22+
}
23+
24+
func (p *Set) Remove(item interface{}) {
25+
p.Lock()
26+
defer p.Unlock()
27+
delete(p.mapInst, item)
28+
}
29+
30+
func (p *Set) PopOne() (item interface{}, ok bool) {
31+
p.Lock()
32+
defer p.Unlock()
33+
if len(p.mapInst) > 0 {
34+
for k := range p.mapInst {
35+
item = k
36+
delete(p.mapInst, k)
37+
break
38+
}
39+
ok = true
40+
} else {
41+
ok = false
42+
}
43+
return
44+
}
45+
46+
func (p *Set) Has(item interface{}) bool {
47+
p.RLock()
48+
defer p.RUnlock()
49+
_, ok := p.mapInst[item]
50+
return ok
51+
}
52+
53+
func (p *Set) Len() int {
54+
return int(len(p.mapInst))
55+
}
56+
57+
func (p *Set) Clear() {
58+
p.Lock()
59+
defer p.Unlock()
60+
p.mapInst = map[interface{}]bool{}
61+
}
62+
63+
func (p *Set) IsEmpty() bool {
64+
if p.Len() == 0 {
65+
return true
66+
}
67+
return false
68+
}
69+
70+
func (p *Set) List() []interface{} {
71+
p.RLock()
72+
defer p.RUnlock()
73+
list := []interface{}{}
74+
75+
for item := range p.mapInst {
76+
list = append(list, item)
77+
}
78+
return list
79+
}

0 commit comments

Comments
 (0)
Please sign in to comment.