Compare commits
No commits in common. "4c80606b56d0c1b121d7091134716a711026fd9a" and "a49257657ed70ef3770d6801f1042e76164e51d2" have entirely different histories.
4c80606b56
...
a49257657e
@ -7,7 +7,6 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||||
)
|
)
|
||||||
@ -22,34 +21,25 @@ type TransactionData struct {
|
|||||||
ActiveAddress string
|
ActiveAddress string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TruncateDecimalString safely truncates (not rounds) a number string to the specified decimal places
|
// TruncateDecimalString safely truncates the input amount to the specified decimal places
|
||||||
func TruncateDecimalString(input string, decimalPlaces int) (string, error) {
|
func TruncateDecimalString(input string, decimalPlaces int) (string, error) {
|
||||||
if _, err := strconv.ParseFloat(input, 64); err != nil {
|
num, ok := new(big.Float).SetString(input)
|
||||||
|
if !ok {
|
||||||
return "", fmt.Errorf("invalid input")
|
return "", fmt.Errorf("invalid input")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split input into integer and fractional parts
|
// Multiply by 10^decimalPlaces
|
||||||
parts := strings.SplitN(input, ".", 2)
|
scale := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimalPlaces)), nil))
|
||||||
intPart := parts[0]
|
scaled := new(big.Float).Mul(num, scale)
|
||||||
var fracPart string
|
|
||||||
|
|
||||||
if len(parts) == 2 {
|
// Truncate by converting to int (chops off decimals)
|
||||||
fracPart = parts[1]
|
intPart, _ := scaled.Int(nil)
|
||||||
}
|
|
||||||
|
|
||||||
// Truncate or pad fractional part
|
// Divide back to get truncated float
|
||||||
if len(fracPart) > decimalPlaces {
|
truncated := new(big.Float).Quo(new(big.Float).SetInt(intPart), scale)
|
||||||
fracPart = fracPart[:decimalPlaces]
|
|
||||||
} else {
|
|
||||||
fracPart = fracPart + strings.Repeat("0", decimalPlaces-len(fracPart))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle zero decimal places
|
// Format with fixed decimals
|
||||||
if decimalPlaces == 0 {
|
return truncated.Text('f', decimalPlaces), nil
|
||||||
return intPart, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s.%s", intPart, fracPart), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
|
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
|
||||||
|
|||||||
@ -22,13 +22,6 @@ func TestTruncateDecimalString(t *testing.T) {
|
|||||||
want: "4.00",
|
want: "4.00",
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "precision test",
|
|
||||||
input: "2.1",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "2.10",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "single decimal",
|
name: "single decimal",
|
||||||
input: "4.1",
|
input: "4.1",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user