36 lines
930 B
Go
36 lines
930 B
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"veyra/services/api/internal/platform/apperror"
|
|
)
|
|
|
|
var moneyPattern = regexp.MustCompile(`^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$`)
|
|
|
|
// ParseMoney converts decimal text into exact cents, rejecting rounding and exponents.
|
|
func ParseMoney(value string) (int64, error) {
|
|
if !moneyPattern.MatchString(value) {
|
|
return 0, apperror.ErrInvalidInput
|
|
}
|
|
whole, fraction, _ := strings.Cut(value, ".")
|
|
units, err := strconv.ParseInt(whole, 10, 64)
|
|
if err != nil {
|
|
return 0, apperror.ErrInvalidInput
|
|
}
|
|
fraction += strings.Repeat("0", 2-len(fraction))
|
|
cents, err := strconv.ParseInt(fraction, 10, 64)
|
|
if err != nil {
|
|
return 0, apperror.ErrInvalidInput
|
|
}
|
|
amount := units*100 + cents
|
|
if amount > 100000000000 {
|
|
return 0, apperror.ErrInvalidInput
|
|
}
|
|
return amount, nil
|
|
}
|
|
|
|
func FormatMoney(minor int64) string { return fmt.Sprintf("%d.%02d", minor/100, minor%100) }
|