trying to fix price

This commit is contained in:
bronkuu 2026-06-15 13:34:18 +02:00
parent 7feee7f8a3
commit 8194cea85e
10 changed files with 145 additions and 54 deletions

33
models/price.go Normal file
View file

@ -0,0 +1,33 @@
package models
import (
"fmt"
"strconv"
"strings"
)
type Price int
func (in Price) String() string {
if in%100 == 0 {
return strconv.Itoa(int(in) / 100)
} else {
price := strconv.Itoa(int(in))
i := len(price) - 2
price = price[:i] + "." + price[i:]
return price
}
}
func ParsePrice(in string) (Price, error) {
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 exactly 2 decimal places after '.'")
}
price, err := strconv.Atoi(before + after)
return Price(price), err
}