Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
9f6eb473
Commit
9f6eb473
authored
Jul 10, 2019
by
aliiohs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
transfer ToString to ToBytes
parent
ae4ac3e9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
21 deletions
+21
-21
decimal.go
math/big/decimal.go
+2
-2
decimal_test.go
math/big/decimal_test.go
+19
-19
No files found.
math/big/decimal.go
View file @
9f6eb473
...
@@ -266,7 +266,7 @@ func (d *Decimal) String() string {
...
@@ -266,7 +266,7 @@ func (d *Decimal) String() string {
tmp
:=
*
d
tmp
:=
*
d
_
=
tmp
.
Round
(
&
tmp
,
int
(
tmp
.
resultFrac
),
ModeHalfEven
)
_
=
tmp
.
Round
(
&
tmp
,
int
(
tmp
.
resultFrac
),
ModeHalfEven
)
//todo terror.Log(errors.Trace(err))
//todo terror.Log(errors.Trace(err))
return
string
(
tmp
.
To
String
())
return
string
(
tmp
.
To
Bytes
())
}
}
func
(
d
*
Decimal
)
stringSize
()
int
{
func
(
d
*
Decimal
)
stringSize
()
int
{
...
@@ -314,7 +314,7 @@ func (d *Decimal) removeTrailingZeros() (lastWordIdx int, digitsFrac int) {
...
@@ -314,7 +314,7 @@ func (d *Decimal) removeTrailingZeros() (lastWordIdx int, digitsFrac int) {
// str - result string
// str - result string
// errCode - eDecOK/eDecTruncate/eDecOverflow
// errCode - eDecOK/eDecTruncate/eDecOverflow
//
//
func
(
d
*
Decimal
)
To
String
()
(
str
[]
byte
)
{
func
(
d
*
Decimal
)
To
Bytes
()
(
str
[]
byte
)
{
str
=
make
([]
byte
,
d
.
stringSize
())
str
=
make
([]
byte
,
d
.
stringSize
())
digitsFrac
:=
int
(
d
.
digitsFrac
)
digitsFrac
:=
int
(
d
.
digitsFrac
)
wordStartIdx
,
digitsInt
:=
d
.
removeLeadingZeros
()
wordStartIdx
,
digitsInt
:=
d
.
removeLeadingZeros
()
...
...
math/big/decimal_test.go
View file @
9f6eb473
...
@@ -39,7 +39,7 @@ func TestFromInt(t *testing.T) {
...
@@ -39,7 +39,7 @@ func TestFromInt(t *testing.T) {
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
dec
:=
NewDecFromInt
(
tt
.
input
)
dec
:=
NewDecFromInt
(
tt
.
input
)
str
:=
dec
.
To
String
()
str
:=
dec
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
str
),
tt
.
output
)
assert
.
Equal
(
t
,
string
(
str
),
tt
.
output
)
}
}
}
}
...
@@ -56,7 +56,7 @@ func TestFromUint(t *testing.T) {
...
@@ -56,7 +56,7 @@ func TestFromUint(t *testing.T) {
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
var
dec
Decimal
var
dec
Decimal
dec
.
FromUint
(
tt
.
input
)
dec
.
FromUint
(
tt
.
input
)
str
:=
dec
.
To
String
()
str
:=
dec
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
str
),
tt
.
output
)
assert
.
Equal
(
t
,
string
(
str
),
tt
.
output
)
}
}
}
}
...
@@ -122,7 +122,7 @@ func TestFromFloat(t *testing.T) {
...
@@ -122,7 +122,7 @@ func TestFromFloat(t *testing.T) {
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
dec
:=
NewDecFromFloatForTest
(
tt
.
f
)
dec
:=
NewDecFromFloatForTest
(
tt
.
f
)
str
:=
dec
.
To
String
()
str
:=
dec
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
str
),
tt
.
s
)
assert
.
Equal
(
t
,
string
(
str
),
tt
.
s
)
}
}
}
}
...
@@ -235,7 +235,7 @@ func TestRemoveTrailingZeros(t *testing.T) {
...
@@ -235,7 +235,7 @@ func TestRemoveTrailingZeros(t *testing.T) {
// calculate the number of digits after point but trailing zero
// calculate the number of digits after point but trailing zero
digitsFracExp
:=
0
digitsFracExp
:=
0
str
:=
string
(
dec
.
To
String
())
str
:=
string
(
dec
.
To
Bytes
())
point
:=
strings
.
Index
(
str
,
"."
)
point
:=
strings
.
Index
(
str
,
"."
)
if
point
!=
-
1
{
if
point
!=
-
1
{
pos
:=
len
(
str
)
-
1
pos
:=
len
(
str
)
-
1
...
@@ -268,7 +268,7 @@ func TestShift(t *testing.T) {
...
@@ -268,7 +268,7 @@ func TestShift(t *testing.T) {
//origin := dec
//origin := dec
err
=
dec
.
Shift
(
ca
.
shift
)
err
=
dec
.
Shift
(
ca
.
shift
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
result
:=
dec
.
To
String
()
result
:=
dec
.
To
Bytes
()
//, Commentf("origin:%s\ndec:%s", origin.String(), string(result))
//, Commentf("origin:%s\ndec:%s", origin.String(), string(result))
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
}
}
...
@@ -398,7 +398,7 @@ func TestRoundWithHalfEven(t *testing.T) {
...
@@ -398,7 +398,7 @@ func TestRoundWithHalfEven(t *testing.T) {
var
rounded
Decimal
var
rounded
Decimal
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
ModeHalfEven
)
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
ModeHalfEven
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
result
:=
rounded
.
To
String
()
result
:=
rounded
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
}
}
}
}
...
@@ -432,7 +432,7 @@ func TestRoundWithTruncate(t *testing.T) {
...
@@ -432,7 +432,7 @@ func TestRoundWithTruncate(t *testing.T) {
var
rounded
Decimal
var
rounded
Decimal
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
ModeTruncate
)
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
ModeTruncate
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
result
:=
rounded
.
To
String
()
result
:=
rounded
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
}
}
}
}
...
@@ -467,7 +467,7 @@ func TestRoundWithCeil(t *testing.T) {
...
@@ -467,7 +467,7 @@ func TestRoundWithCeil(t *testing.T) {
var
rounded
Decimal
var
rounded
Decimal
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
modeCeiling
)
err
:=
dec
.
Round
(
&
rounded
,
ca
.
scale
,
modeCeiling
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
result
:=
rounded
.
To
String
()
result
:=
rounded
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
}
}
}
}
...
@@ -494,7 +494,7 @@ func TestFromString(t *testing.T) {
...
@@ -494,7 +494,7 @@ func TestFromString(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
assert
.
Equal
(
t
,
err
,
ca
.
err
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
}
}
result
:=
string
(
dec
.
To
String
())
result
:=
string
(
dec
.
To
Bytes
())
assert
.
Equal
(
t
,
result
,
ca
.
output
)
assert
.
Equal
(
t
,
result
,
ca
.
output
)
}
}
wordBufLen
=
1
wordBufLen
=
1
...
@@ -506,13 +506,13 @@ func TestFromString(t *testing.T) {
...
@@ -506,13 +506,13 @@ func TestFromString(t *testing.T) {
var
dec
Decimal
var
dec
Decimal
err
:=
dec
.
FromBytes
([]
byte
(
ca
.
input
))
err
:=
dec
.
FromBytes
([]
byte
(
ca
.
input
))
assert
.
Equal
(
t
,
err
,
ca
.
err
)
assert
.
Equal
(
t
,
err
,
ca
.
err
)
result
:=
string
(
dec
.
To
String
())
result
:=
string
(
dec
.
To
Bytes
())
assert
.
Equal
(
t
,
result
,
ca
.
output
)
assert
.
Equal
(
t
,
result
,
ca
.
output
)
}
}
wordBufLen
=
maxWordBufLen
wordBufLen
=
maxWordBufLen
}
}
func
TestTo
String
(
t
*
testing
.
T
)
{
func
TestTo
Bytes
(
t
*
testing
.
T
)
{
type
tcase
struct
{
type
tcase
struct
{
input
string
input
string
output
string
output
string
...
@@ -525,7 +525,7 @@ func TestToString(t *testing.T) {
...
@@ -525,7 +525,7 @@ func TestToString(t *testing.T) {
for
_
,
ca
:=
range
tests
{
for
_
,
ca
:=
range
tests
{
var
dec
Decimal
var
dec
Decimal
_
=
dec
.
FromBytes
([]
byte
(
ca
.
input
))
_
=
dec
.
FromBytes
([]
byte
(
ca
.
input
))
result
:=
dec
.
To
String
()
result
:=
dec
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
assert
.
Equal
(
t
,
string
(
result
),
ca
.
output
)
}
}
}
}
...
@@ -564,7 +564,7 @@ func TestToBinFromBin(t *testing.T) {
...
@@ -564,7 +564,7 @@ func TestToBinFromBin(t *testing.T) {
var
dec2
Decimal
var
dec2
Decimal
_
,
err
=
dec2
.
FromBin
(
buf
,
ca
.
precision
,
ca
.
frac
)
_
,
err
=
dec2
.
FromBin
(
buf
,
ca
.
precision
,
ca
.
frac
)
assert
.
Equal
(
t
,
err
,
nil
)
assert
.
Equal
(
t
,
err
,
nil
)
str
:=
dec2
.
To
String
()
str
:=
dec2
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
str
),
ca
.
output
)
assert
.
Equal
(
t
,
string
(
str
),
ca
.
output
)
}
}
var
dec
Decimal
var
dec
Decimal
...
@@ -637,7 +637,7 @@ func TestMaxDecimal(t *testing.T) {
...
@@ -637,7 +637,7 @@ func TestMaxDecimal(t *testing.T) {
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
var
dec
Decimal
var
dec
Decimal
maxDecimal
(
tt
.
prec
,
tt
.
frac
,
&
dec
)
maxDecimal
(
tt
.
prec
,
tt
.
frac
,
&
dec
)
str
:=
dec
.
To
String
()
str
:=
dec
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
str
),
tt
.
result
)
assert
.
Equal
(
t
,
string
(
str
),
tt
.
result
)
}
}
}
}
...
@@ -656,7 +656,7 @@ func TestNeg(t *testing.T) {
...
@@ -656,7 +656,7 @@ func TestNeg(t *testing.T) {
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
a
:=
NewDecFromStringForTest
(
tt
.
a
)
a
:=
NewDecFromStringForTest
(
tt
.
a
)
negResult
:=
DecimalNeg
(
a
)
negResult
:=
DecimalNeg
(
a
)
result
:=
negResult
.
To
String
()
result
:=
negResult
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
}
}
}
}
...
@@ -691,7 +691,7 @@ func TestAdd(t *testing.T) {
...
@@ -691,7 +691,7 @@ func TestAdd(t *testing.T) {
var
sum
Decimal
var
sum
Decimal
err
:=
DecimalAdd
(
a
,
b
,
&
sum
)
err
:=
DecimalAdd
(
a
,
b
,
&
sum
)
assert
.
Equal
(
t
,
err
,
tt
.
err
)
assert
.
Equal
(
t
,
err
,
tt
.
err
)
result
:=
sum
.
To
String
()
result
:=
sum
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
}
}
}
}
...
@@ -725,7 +725,7 @@ func TestSub(t *testing.T) {
...
@@ -725,7 +725,7 @@ func TestSub(t *testing.T) {
b
.
FromBytes
([]
byte
(
tt
.
b
))
b
.
FromBytes
([]
byte
(
tt
.
b
))
err
:=
DecimalSub
(
&
a
,
&
b
,
&
sum
)
err
:=
DecimalSub
(
&
a
,
&
b
,
&
sum
)
assert
.
Equal
(
t
,
err
,
tt
.
err
)
assert
.
Equal
(
t
,
err
,
tt
.
err
)
result
:=
sum
.
To
String
()
result
:=
sum
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
}
}
}
}
...
@@ -794,7 +794,7 @@ func TestDivMod(t *testing.T) {
...
@@ -794,7 +794,7 @@ func TestDivMod(t *testing.T) {
if
tt
.
err
==
ErrDivByZero
{
if
tt
.
err
==
ErrDivByZero
{
continue
continue
}
}
result
:=
to
.
To
String
()
result
:=
to
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
}
}
...
@@ -816,7 +816,7 @@ func TestDivMod(t *testing.T) {
...
@@ -816,7 +816,7 @@ func TestDivMod(t *testing.T) {
if
tt
.
err
==
ErrDivByZero
{
if
tt
.
err
==
ErrDivByZero
{
continue
continue
}
}
result
:=
to
.
To
String
()
result
:=
to
.
To
Bytes
()
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
assert
.
Equal
(
t
,
string
(
result
),
tt
.
result
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment