Skip to content

Commit c32ccec

Browse files
Merge pull request #119 from Peersyst/binary-codec/fix/amount-transcoding
[TA-4223]: Amounts transcoding fix
2 parents 66cc52f + eac0270 commit c32ccec

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pkg/big-decimal/big_decimal.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
const (
1313
AllowedCharacters = "0123456789.-eE"
1414
BigDecRegEx = "-?(?:[0|1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?"
15+
Precision = 128
1516
)
1617

1718
var (
@@ -28,24 +29,32 @@ type BigDecimal struct {
2829
}
2930

3031
func (bd *BigDecimal) GetScaledValue() string {
31-
unscaled, _ := new(big.Float).SetString(bd.UnscaledValue)
32+
if bd.UnscaledValue == "" {
33+
return "0"
34+
}
3235

33-
scalingFactor := new(big.Float).SetFloat64(1)
36+
// Use SetPrec to maintain full precision
37+
unscaled := new(big.Float).SetPrec(Precision) // Use high precision to avoid scientific notation
38+
unscaled, _ = unscaled.SetString(bd.UnscaledValue)
39+
40+
scalingFactor := new(big.Float).SetPrec(Precision).SetFloat64(1)
3441
for i := 0; i < abs(bd.Scale); i++ {
3542
scalingFactor.Mul(scalingFactor, big.NewFloat(10))
3643
}
3744

3845
var scaledValue *big.Float
3946
if bd.Scale >= 0 {
40-
scaledValue = new(big.Float).Mul(unscaled, scalingFactor)
47+
scaledValue = new(big.Float).SetPrec(Precision).Mul(unscaled, scalingFactor)
4148
} else {
42-
scaledValue = new(big.Float).Quo(unscaled, scalingFactor)
49+
scaledValue = new(big.Float).SetPrec(Precision).Quo(unscaled, scalingFactor)
4350
}
4451

4552
if bd.Sign == 1 {
4653
scaledValue.Neg(scaledValue)
4754
}
48-
return strings.TrimSuffix(strings.TrimRight(scaledValue.Text('f', bd.Scale), "0"), ".")
55+
56+
// Force format without scientific notation
57+
return strings.TrimSuffix(strings.TrimRight(scaledValue.Text('f', abs(bd.Scale)), "0"), ".")
4958
}
5059

5160
func abs(x int) int {

pkg/big-decimal/big_decimal_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ func TestGetScaledValue(t *testing.T) {
6262
},
6363
expected: "-23.005",
6464
},
65+
{
66+
description: "large value",
67+
bd: &BigDecimal{
68+
Scale: 7,
69+
UnscaledValue: "999999999999999",
70+
Precision: 16,
71+
Sign: 0,
72+
},
73+
expected: "9999999999999990000000",
74+
},
6575
}
6676

6777
for _, tc := range tt {

0 commit comments

Comments
 (0)