32 lines
574 B
Go
32 lines
574 B
Go
package firstrechargereward
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func parseAmountCents(raw string) (int64, error) {
|
|
value := strings.TrimSpace(raw)
|
|
if value == "" {
|
|
return 0, nil
|
|
}
|
|
value = strings.TrimPrefix(value, "$")
|
|
parsed, err := strconv.ParseFloat(value, 64)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if parsed < 0 {
|
|
return 0, fmt.Errorf("amount must not be negative")
|
|
}
|
|
return int64(math.Round(parsed * 100)), nil
|
|
}
|
|
|
|
func formatAmountCents(cents int64) string {
|
|
if cents <= 0 {
|
|
return "0.00"
|
|
}
|
|
return fmt.Sprintf("%.2f", float64(cents)/100)
|
|
}
|