@@ -33,14 +33,17 @@ func TestFloat(t *testing.T) {
3333 {0.01 , 1 , "0" },
3434 {0.01 , 2 , "0.01" },
3535 {0.01 , 4 , "0.01" },
36+ {- 1.23456789 , 2 , "-1.23" },
3637 {1.23456789 , 2 , "1.23" },
3738 {1.23456789 , 3 , "1.235" },
3839 {1.23456789 , 3 , "1.235" },
3940 {123456.7777 , 1 , "123456.8" },
4041 {123456.7777 , 2 , "123456.78" },
4142 {123456.1010 , 4 , "123456.101" },
4243 {123456.1010 , 2 , "123456.1" },
44+ {- 123456.1010 , 1 , "-123456.1" },
4345 {123456.1010 , 1 , "123456.1" },
46+ {- 123456.1010 , 0 , "-123456" },
4447 {123456.1010 , 0 , "123456" },
4548 }
4649
@@ -52,6 +55,34 @@ func TestFloat(t *testing.T) {
5255 }
5356}
5457
58+ func TestPercent (t * testing.T ) {
59+ tests := []struct {
60+ a , b float64
61+ expected string
62+ }{
63+ {a : 0 , b : 0 , expected : "" },
64+ {a : 0 , b : 100 , expected : "0%" },
65+ {a : 0.0001 , b : 100.0 , expected : "~0%" },
66+ {a : 0.044 , b : 100 , expected : "~0%" },
67+ {a : 0.05 , b : 100 , expected : "0.1%" },
68+ {a : 0.1234 , b : 100.0 , expected : "0.1%" },
69+ {a : - 0.1234 , b : 100.0 , expected : "-0.1%" },
70+ {a : 0.05 , b : 100.0 , expected : "0.1%" },
71+ {a : 9.95 , b : 100.0 , expected : "10%" },
72+ {a : 9.94 , b : 100.0 , expected : "9.9%" },
73+ {a : - 9.95 , b : 100.0 , expected : "-10%" },
74+ {a : - 9.94 , b : 100.0 , expected : "-9.9%" },
75+ {a : 10.52345 , b : 100.0 , expected : "11%" },
76+ }
77+
78+ for _ , test := range tests {
79+ result := string (Percent (test .a , test .b ))
80+ if result != test .expected {
81+ t .Errorf ("Percent(%f,%f) = %s; expected %s" , test .a , test .b , result , test .expected )
82+ }
83+ }
84+ }
85+
5586func ExampleFloat () {
5687 fmt .Println (Float (100.1234 , 3 ))
5788 fmt .Println (Float (100.12 , 3 ))
@@ -63,3 +94,17 @@ func ExampleFloat() {
6394 // 100.1
6495 // 100
6596}
97+
98+ func ExamplePercent () {
99+ fmt .Println (Percent (uint64 (0 ), uint64 (10000 )))
100+ fmt .Println (Percent (uint64 (1 ), uint64 (10000 )))
101+ fmt .Println (Percent (uint64 (12 ), uint64 (10000 )))
102+ fmt .Println (Percent (uint64 (123 ), uint64 (10000 )))
103+ fmt .Println (Percent (uint64 (1234 ), uint64 (10000 )))
104+ // Output:
105+ // 0%
106+ // ~0%
107+ // 0.1%
108+ // 1.2%
109+ // 12%
110+ }
0 commit comments