1package strconv
2
3// AppendPrice will append an int64 formatted as a price, where the int64 is the price in cents.
4// It does not display whether a price is negative or not.
5func AppendPrice(b []byte, price int64, dec bool, milSeparator byte, decSeparator byte) []byte {
6	if price < 0 {
7		if price == -9223372036854775808 {
8			x := []byte("92 233 720 368 547 758 08")
9			x[2] = milSeparator
10			x[6] = milSeparator
11			x[10] = milSeparator
12			x[14] = milSeparator
13			x[18] = milSeparator
14			x[22] = decSeparator
15			return append(b, x...)
16		}
17		price = -price
18	}
19
20	// rounding
21	if !dec {
22		firstDec := (price / 10) % 10
23		if firstDec >= 5 {
24			price += 100
25		}
26	}
27
28	// calculate size
29	n := LenInt(price) - 2
30	if n > 0 {
31		n += (n - 1) / 3 // mil separator
32	} else {
33		n = 1
34	}
35	if dec {
36		n += 2 + 1 // decimals + dec separator
37	}
38
39	// resize byte slice
40	i := len(b)
41	if i+n > cap(b) {
42		b = append(b, make([]byte, n)...)
43	} else {
44		b = b[:i+n]
45	}
46
47	// print fractional-part
48	i += n - 1
49	if dec {
50		for j := 0; j < 2; j++ {
51			c := byte(price%10) + '0'
52			price /= 10
53			b[i] = c
54			i--
55		}
56		b[i] = decSeparator
57		i--
58	} else {
59		price /= 100
60	}
61
62	if price == 0 {
63		b[i] = '0'
64		return b
65	}
66
67	// print integer-part
68	j := 0
69	for price > 0 {
70		if j == 3 {
71			b[i] = milSeparator
72			i--
73			j = 0
74		}
75
76		c := byte(price%10) + '0'
77		price /= 10
78		b[i] = c
79		i--
80		j++
81	}
82	return b
83}
84