gitee.com/quant1x/gox@v1.21.2/api/number_decimals_test.go (about) 1 package api 2 3 import ( 4 _ "cmp" 5 "fmt" 6 "math" 7 "testing" 8 ) 9 10 func TestDecimal0(t *testing.T) { 11 f := -0.116 12 fmt.Println(math.Signbit(f)) 13 fmt.Println(f) 14 fmt.Printf("%.2f\n", f) 15 } 16 17 func TestDecimal(t *testing.T) { 18 type args struct { 19 value float64 20 digits []int 21 } 22 tests := []struct { 23 name string 24 args args 25 want float64 26 }{ 27 { 28 name: "T9.8", 29 args: args{ 30 value: 9.825, 31 digits: []int{0}, 32 }, 33 want: 10, 34 }, 35 { 36 name: "T9.825", 37 args: args{ 38 value: 9.825, 39 digits: []int{2}, 40 }, 41 want: 9.83, 42 }, 43 { 44 name: "T0.116", 45 args: args{ 46 value: 0.116, 47 digits: []int{2}, 48 }, 49 want: 0.12, 50 }, 51 { 52 name: "T0.11", 53 args: args{ 54 value: 0.1115355659035776, 55 digits: []int{2}, 56 }, 57 want: 0.11, 58 }, 59 { 60 name: "T-0.11", 61 args: args{ 62 value: -0.1115355659035776, 63 digits: []int{2}, 64 }, 65 want: -0.11, 66 }, 67 } 68 for _, tt := range tests { 69 t.Run(tt.name, func(t *testing.T) { 70 if got := Decimal(tt.args.value, tt.args.digits...); got != tt.want { 71 t.Errorf("Decimal() = %v, want %v", got, tt.want) 72 } 73 }) 74 } 75 }