diff --git a/src/rest/routes.go b/src/rest/routes.go index 08e1664..d386340 100644 --- a/src/rest/routes.go +++ b/src/rest/routes.go @@ -5,6 +5,7 @@ import ( "controtto/src/app/managing" "controtto/src/app/querying" "controtto/src/app/trading" + "controtto/src/rest/ui" "fmt" "log" @@ -15,6 +16,7 @@ import ( func Run(c *config.Manager, m *managing.Service, q *querying.Service, t *trading.Service) { engine := html.New("./views", ".html") engine.Debug(true) + engine.AddFunc("displayPrice", ui.DisplayPrice) app := fiber.New(fiber.Config{ Views: engine, ErrorHandler: func(c *fiber.Ctx, err error) error { diff --git a/src/rest/ui/parsePrice.go b/src/rest/ui/parsePrice.go new file mode 100644 index 0000000..bcf8bcd --- /dev/null +++ b/src/rest/ui/parsePrice.go @@ -0,0 +1,26 @@ +package ui + +import ( + "fmt" + "strings" +) + +func DisplayPrice(v float64) string { + s := fmt.Sprintf("%.2f", v) + n := len(s) + if n <= 6 { // e.g. 999.99 + return s + } + // Insert commas for thousands + parts := strings.Split(s, ".") + intPart := parts[0] + decPart := parts[1] + var out []byte + for i, c := range intPart { + if i != 0 && (len(intPart)-i)%3 == 0 { + out = append(out, ',') + } + out = append(out, byte(c)) + } + return string(out) + "." + decPart +} diff --git a/views/pairCards.html b/views/pairCards.html index ec49369..2c074ab 100644 --- a/views/pairCards.html +++ b/views/pairCards.html @@ -29,7 +29,10 @@