package models import ( "fmt" "strconv" "strings" ) type Price int func (in Price) String() string { if in%100 == 0 { return strconv.Itoa(int(in) / 100) } price := strconv.Itoa(int(in)) i := len(price) - 2 return price[:i] + "." + price[i:] } func ParsePrice(in string) (Price, error) { in = strings.ReplaceAll(in, ",", ".") if !strings.Contains(in, ".") { price, err := strconv.Atoi(in) return Price(price) * 100, err } before, after, _ := strings.Cut(in, ".") if len(after) > 2 { return 0, fmt.Errorf("Wrong format, requires no more than 2 decimal places after the separator") } price, err := strconv.Atoi(before + after) if len(after) < 2 { price *= 10 } if len(after) < 1 { price *= 10 } return Price(price), err }