mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 08:15:15 +00:00
add FormatString test case and fix FormatString
This commit is contained in:
parent
10612cdfa9
commit
99b025dd5c
|
@ -95,8 +95,9 @@ func (v Value) String() string {
|
|||
}
|
||||
|
||||
func (v Value) FormatString(prec int) string {
|
||||
result := strconv.FormatFloat(float64(v)/DefaultPow, 'f', prec+1, 64)
|
||||
return result[:len(result)-1]
|
||||
pow := math.Pow10(prec)
|
||||
return strconv.FormatFloat(
|
||||
math.Trunc(float64(v)/DefaultPow * pow) / pow, 'f', prec, 64)
|
||||
}
|
||||
|
||||
func (v Value) Percentage() string {
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package fixedpoint
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const Delta = 1e-9
|
||||
|
@ -70,6 +71,38 @@ func TestNew(t *testing.T) {
|
|||
assert.Equal(t, "0.10%", f.FormatPercentage(2))
|
||||
}
|
||||
|
||||
func TestFormatString(t *testing.T) {
|
||||
testCases := []struct {
|
||||
value Value
|
||||
prec int
|
||||
out string
|
||||
}{
|
||||
{
|
||||
value: NewFromFloat(0.001),
|
||||
prec: 8,
|
||||
out: "0.00100000",
|
||||
},
|
||||
{
|
||||
value: NewFromFloat(0.123456789),
|
||||
prec: 4,
|
||||
out: "0.1234",
|
||||
},
|
||||
{
|
||||
value: NewFromFloat(0.123456789),
|
||||
prec: 5,
|
||||
out: "0.12345",
|
||||
},
|
||||
{
|
||||
value: NewFromFloat(20.0),
|
||||
prec: 0,
|
||||
out: "20",
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
assert.Equal(t, testCase.out, testCase.value.FormatString(testCase.prec))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRound(t *testing.T) {
|
||||
f := NewFromFloat(1.2345)
|
||||
f = f.Round(0, Down)
|
||||
|
|
Loading…
Reference in New Issue
Block a user