Compare commits

..

No commits in common. "4c80606b56d0c1b121d7091134716a711026fd9a" and "a49257657ed70ef3770d6801f1042e76164e51d2" have entirely different histories.

2 changed files with 12 additions and 29 deletions

View File

@ -7,7 +7,6 @@ import (
"math/big"
"reflect"
"strconv"
"strings"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
)
@ -22,34 +21,25 @@ type TransactionData struct {
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) {
if _, err := strconv.ParseFloat(input, 64); err != nil {
num, ok := new(big.Float).SetString(input)
if !ok {
return "", fmt.Errorf("invalid input")
}
// Split input into integer and fractional parts
parts := strings.SplitN(input, ".", 2)
intPart := parts[0]
var fracPart string
// Multiply by 10^decimalPlaces
scale := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimalPlaces)), nil))
scaled := new(big.Float).Mul(num, scale)
if len(parts) == 2 {
fracPart = parts[1]
}
// Truncate by converting to int (chops off decimals)
intPart, _ := scaled.Int(nil)
// Truncate or pad fractional part
if len(fracPart) > decimalPlaces {
fracPart = fracPart[:decimalPlaces]
} else {
fracPart = fracPart + strings.Repeat("0", decimalPlaces-len(fracPart))
}
// Divide back to get truncated float
truncated := new(big.Float).Quo(new(big.Float).SetInt(intPart), scale)
// Handle zero decimal places
if decimalPlaces == 0 {
return intPart, nil
}
return fmt.Sprintf("%s.%s", intPart, fracPart), nil
// Format with fixed decimals
return truncated.Text('f', decimalPlaces), nil
}
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {

View File

@ -22,13 +22,6 @@ func TestTruncateDecimalString(t *testing.T) {
want: "4.00",
expectError: false,
},
{
name: "precision test",
input: "2.1",
decimalPlaces: 2,
want: "2.10",
expectError: false,
},
{
name: "single decimal",
input: "4.1",