repos
/ orchard main

orchard

mirror

Every site I host, in one repo, along with the Cloudflare Tunnel and Caddy that front them. It's all Go, Vite, and SQLite, and it runs on a desktop at home with nothing listening on an inbound port.

blogbuncaddycloudflare-tunneldockergogolanghomelabhtml-templatemonorepoself-hostedseosqlitestatic-sitetypstuptime-monitoringviteweb-analytics

754 B · 41 lines · Go Raw History
 1package main
 2
 3import (
 4	"fmt"
 5	"strconv"
 6	"strings"
 7)
 8
 9// formatNum groups thousands with commas. It takes any, because a template
10// handing an int to a function declared int64 fails at render time rather than
11// at build time, which 500s a page every test still passes.
12func formatNum(v any) string {
13	var n int64
14	switch t := v.(type) {
15	case int:
16		n = int64(t)
17	case int64:
18		n = t
19	case float64:
20		n = int64(t)
21	default:
22		return fmt.Sprint(v)
23	}
24
25	s := strconv.FormatInt(n, 10)
26	negative := strings.HasPrefix(s, "-")
27	s = strings.TrimPrefix(s, "-")
28
29	var b strings.Builder
30	for i, digit := range s {
31		if i > 0 && (len(s)-i)%3 == 0 {
32			b.WriteByte(',')
33		}
34		b.WriteRune(digit)
35	}
36	if negative {
37		return "-" + b.String()
38	}
39	return b.String()
40}