This commit is contained in:
Pavel Flegr 2026-04-08 17:53:30 +02:00
commit ef105e1cac
22 changed files with 3001 additions and 0 deletions

View file

@ -0,0 +1,79 @@
package platform
import (
"fmt"
"strconv"
"strings"
)
func FormatCZK(amountMinor int64) string {
sign := ""
if amountMinor < 0 {
sign = "-"
amountMinor = -amountMinor
}
whole := amountMinor / 100
fraction := amountMinor % 100
return fmt.Sprintf("%s%s,%02d CZK", sign, formatThousands(whole), fraction)
}
func ParseCZK(input string) (int64, error) {
clean := strings.TrimSpace(strings.ReplaceAll(input, " ", ""))
clean = strings.ReplaceAll(clean, ",", ".")
if clean == "" {
return 0, fmt.Errorf("amount is required")
}
parts := strings.Split(clean, ".")
if len(parts) > 2 {
return 0, fmt.Errorf("invalid amount")
}
whole, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid whole amount")
}
var fraction int64
if len(parts) == 2 {
frac := parts[1]
if len(frac) > 2 {
return 0, fmt.Errorf("too many decimal places")
}
if len(frac) == 1 {
frac += "0"
}
if len(frac) == 0 {
frac = "00"
}
fraction, err = strconv.ParseInt(frac, 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid decimal amount")
}
}
if whole < 0 || fraction < 0 {
return 0, fmt.Errorf("amount must be positive")
}
return whole*100 + fraction, nil
}
func formatThousands(value int64) string {
plain := strconv.FormatInt(value, 10)
if len(plain) <= 3 {
return plain
}
var parts []string
for len(plain) > 3 {
parts = append([]string{plain[len(plain)-3:]}, parts...)
plain = plain[:len(plain)-3]
}
parts = append([]string{plain}, parts...)
return strings.Join(parts, " ")
}