Skip to content

Commit 9f6eb47

Browse files
committed
transfer ToString to ToBytes
1 parent ae4ac3e commit 9f6eb47

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

math/big/decimal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (d *Decimal) String() string {
266266
tmp := *d
267267
_ = tmp.Round(&tmp, int(tmp.resultFrac), ModeHalfEven)
268268
//todo terror.Log(errors.Trace(err))
269-
return string(tmp.ToString())
269+
return string(tmp.ToBytes())
270270
}
271271

272272
func (d *Decimal) stringSize() int {
@@ -314,7 +314,7 @@ func (d *Decimal) removeTrailingZeros() (lastWordIdx int, digitsFrac int) {
314314
// str - result string
315315
// errCode - eDecOK/eDecTruncate/eDecOverflow
316316
//
317-
func (d *Decimal) ToString() (str []byte) {
317+
func (d *Decimal) ToBytes() (str []byte) {
318318
str = make([]byte, d.stringSize())
319319
digitsFrac := int(d.digitsFrac)
320320
wordStartIdx, digitsInt := d.removeLeadingZeros()

math/big/decimal_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestFromInt(t *testing.T) {
3939
}
4040
for _, tt := range tests {
4141
dec := NewDecFromInt(tt.input)
42-
str := dec.ToString()
42+
str := dec.ToBytes()
4343
assert.Equal(t, string(str), tt.output)
4444
}
4545
}
@@ -56,7 +56,7 @@ func TestFromUint(t *testing.T) {
5656
for _, tt := range tests {
5757
var dec Decimal
5858
dec.FromUint(tt.input)
59-
str := dec.ToString()
59+
str := dec.ToBytes()
6060
assert.Equal(t, string(str), tt.output)
6161
}
6262
}
@@ -122,7 +122,7 @@ func TestFromFloat(t *testing.T) {
122122
}
123123
for _, tt := range tests {
124124
dec := NewDecFromFloatForTest(tt.f)
125-
str := dec.ToString()
125+
str := dec.ToBytes()
126126
assert.Equal(t, string(str), tt.s)
127127
}
128128
}
@@ -235,7 +235,7 @@ func TestRemoveTrailingZeros(t *testing.T) {
235235

236236
// calculate the number of digits after point but trailing zero
237237
digitsFracExp := 0
238-
str := string(dec.ToString())
238+
str := string(dec.ToBytes())
239239
point := strings.Index(str, ".")
240240
if point != -1 {
241241
pos := len(str) - 1
@@ -268,7 +268,7 @@ func TestShift(t *testing.T) {
268268
//origin := dec
269269
err = dec.Shift(ca.shift)
270270
assert.Equal(t, err, ca.err)
271-
result := dec.ToString()
271+
result := dec.ToBytes()
272272
//, Commentf("origin:%s\ndec:%s", origin.String(), string(result))
273273
assert.Equal(t, string(result), ca.output)
274274
}
@@ -398,7 +398,7 @@ func TestRoundWithHalfEven(t *testing.T) {
398398
var rounded Decimal
399399
err := dec.Round(&rounded, ca.scale, ModeHalfEven)
400400
assert.Equal(t, err, ca.err)
401-
result := rounded.ToString()
401+
result := rounded.ToBytes()
402402
assert.Equal(t, string(result), ca.output)
403403
}
404404
}
@@ -432,7 +432,7 @@ func TestRoundWithTruncate(t *testing.T) {
432432
var rounded Decimal
433433
err := dec.Round(&rounded, ca.scale, ModeTruncate)
434434
assert.Equal(t, err, ca.err)
435-
result := rounded.ToString()
435+
result := rounded.ToBytes()
436436
assert.Equal(t, string(result), ca.output)
437437
}
438438
}
@@ -467,7 +467,7 @@ func TestRoundWithCeil(t *testing.T) {
467467
var rounded Decimal
468468
err := dec.Round(&rounded, ca.scale, modeCeiling)
469469
assert.Equal(t, err, ca.err)
470-
result := rounded.ToString()
470+
result := rounded.ToBytes()
471471
assert.Equal(t, string(result), ca.output)
472472
}
473473
}
@@ -494,7 +494,7 @@ func TestFromString(t *testing.T) {
494494
if err != nil {
495495
assert.Equal(t, err, ca.err)
496496
}
497-
result := string(dec.ToString())
497+
result := string(dec.ToBytes())
498498
assert.Equal(t, result, ca.output)
499499
}
500500
wordBufLen = 1
@@ -506,13 +506,13 @@ func TestFromString(t *testing.T) {
506506
var dec Decimal
507507
err := dec.FromBytes([]byte(ca.input))
508508
assert.Equal(t, err, ca.err)
509-
result := string(dec.ToString())
509+
result := string(dec.ToBytes())
510510
assert.Equal(t, result, ca.output)
511511
}
512512
wordBufLen = maxWordBufLen
513513
}
514514

515-
func TestToString(t *testing.T) {
515+
func TestToBytes(t *testing.T) {
516516
type tcase struct {
517517
input string
518518
output string
@@ -525,7 +525,7 @@ func TestToString(t *testing.T) {
525525
for _, ca := range tests {
526526
var dec Decimal
527527
_ = dec.FromBytes([]byte(ca.input))
528-
result := dec.ToString()
528+
result := dec.ToBytes()
529529
assert.Equal(t, string(result), ca.output)
530530
}
531531
}
@@ -564,7 +564,7 @@ func TestToBinFromBin(t *testing.T) {
564564
var dec2 Decimal
565565
_, err = dec2.FromBin(buf, ca.precision, ca.frac)
566566
assert.Equal(t, err, nil)
567-
str := dec2.ToString()
567+
str := dec2.ToBytes()
568568
assert.Equal(t, string(str), ca.output)
569569
}
570570
var dec Decimal
@@ -637,7 +637,7 @@ func TestMaxDecimal(t *testing.T) {
637637
for _, tt := range tests {
638638
var dec Decimal
639639
maxDecimal(tt.prec, tt.frac, &dec)
640-
str := dec.ToString()
640+
str := dec.ToBytes()
641641
assert.Equal(t, string(str), tt.result)
642642
}
643643
}
@@ -656,7 +656,7 @@ func TestNeg(t *testing.T) {
656656
for _, tt := range tests {
657657
a := NewDecFromStringForTest(tt.a)
658658
negResult := DecimalNeg(a)
659-
result := negResult.ToString()
659+
result := negResult.ToBytes()
660660
assert.Equal(t, string(result), tt.result)
661661
}
662662
}
@@ -691,7 +691,7 @@ func TestAdd(t *testing.T) {
691691
var sum Decimal
692692
err := DecimalAdd(a, b, &sum)
693693
assert.Equal(t, err, tt.err)
694-
result := sum.ToString()
694+
result := sum.ToBytes()
695695
assert.Equal(t, string(result), tt.result)
696696
}
697697
}
@@ -725,7 +725,7 @@ func TestSub(t *testing.T) {
725725
b.FromBytes([]byte(tt.b))
726726
err := DecimalSub(&a, &b, &sum)
727727
assert.Equal(t, err, tt.err)
728-
result := sum.ToString()
728+
result := sum.ToBytes()
729729
assert.Equal(t, string(result), tt.result)
730730
}
731731
}
@@ -794,7 +794,7 @@ func TestDivMod(t *testing.T) {
794794
if tt.err == ErrDivByZero {
795795
continue
796796
}
797-
result := to.ToString()
797+
result := to.ToBytes()
798798
assert.Equal(t, string(result), tt.result)
799799
}
800800

@@ -816,7 +816,7 @@ func TestDivMod(t *testing.T) {
816816
if tt.err == ErrDivByZero {
817817
continue
818818
}
819-
result := to.ToString()
819+
result := to.ToBytes()
820820
assert.Equal(t, string(result), tt.result)
821821
}
822822

0 commit comments

Comments
 (0)