clerk/internal/decimal/decimal.go (view raw)
| 1 | package decimal |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math/big" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | type Decimal struct { |
| 10 | scale int |
| 11 | coeff *big.Int |
| 12 | } |
| 13 | |
| 14 | func FromInt(v int64) Decimal { |
| 15 | if v == 0 { |
| 16 | return Decimal{} |
| 17 | } |
| 18 | return Decimal{coeff: big.NewInt(v)} |
| 19 | } |
| 20 | |
| 21 | func FromString(s string) (Decimal, error) { |
| 22 | original := s |
| 23 | if s == "" { |
| 24 | return Decimal{}, fmt.Errorf("can't convert %s to decimal", original) |
| 25 | } |
| 26 | |
| 27 | sign := 1 |
| 28 | if s[0] == '+' || s[0] == '-' { |
| 29 | if s[0] == '-' { |
| 30 | sign = -1 |
| 31 | } |
| 32 | s = s[1:] |
| 33 | } |
| 34 | |
| 35 | if s == "" { |
| 36 | return Decimal{}, fmt.Errorf("can't convert %s to decimal", original) |
| 37 | } |
| 38 | |
| 39 | firstDot := strings.IndexByte(s, '.') |
| 40 | lastDot := strings.LastIndexByte(s, '.') |
| 41 | if firstDot != lastDot { |
| 42 | return Decimal{}, fmt.Errorf("can't convert %s to decimal", original) |
| 43 | } |
| 44 | |
| 45 | intPart := s |
| 46 | fracPart := "" |
| 47 | if firstDot >= 0 { |
| 48 | intPart = s[:firstDot] |
| 49 | fracPart = s[firstDot+1:] |
| 50 | } |
| 51 | |
| 52 | if len(intPart)+len(fracPart) == 0 { |
| 53 | return Decimal{}, fmt.Errorf("can't convert %s to decimal", original) |
| 54 | } |
| 55 | |
| 56 | for i := 0; i < len(intPart); i++ { |
| 57 | if intPart[i] < '0' || intPart[i] > '9' { |
| 58 | return Decimal{}, fmt.Errorf("can't convert %s to decimal", original) |
| 59 | } |
| 60 | } |
| 61 | for i := 0; i < len(fracPart); i++ { |
| 62 | if fracPart[i] < '0' || fracPart[i] > '9' { |
| 63 | return Decimal{}, fmt.Errorf("can't convert %s to decimal", original) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | coeffDigits := intPart + fracPart |
| 68 | coeff := new(big.Int) |
| 69 | if _, ok := coeff.SetString(coeffDigits, 10); !ok { |
| 70 | return Decimal{}, fmt.Errorf("can't convert %s to decimal", original) |
| 71 | } |
| 72 | if sign < 0 && coeff.Sign() != 0 { |
| 73 | coeff.Neg(coeff) |
| 74 | } |
| 75 | |
| 76 | out := Decimal{coeff: coeff, scale: len(fracPart)} |
| 77 | return out.normalized(), nil |
| 78 | } |
| 79 | |
| 80 | func (d Decimal) String() string { |
| 81 | if d.coeff == nil || d.coeff.Sign() == 0 { |
| 82 | return "0" |
| 83 | } |
| 84 | |
| 85 | abs := new(big.Int).Set(d.coeff) |
| 86 | sign := "" |
| 87 | if abs.Sign() < 0 { |
| 88 | sign = "-" |
| 89 | abs.Abs(abs) |
| 90 | } |
| 91 | |
| 92 | digits := abs.String() |
| 93 | if d.scale == 0 { |
| 94 | return sign + digits |
| 95 | } |
| 96 | |
| 97 | if len(digits) <= d.scale { |
| 98 | digits = strings.Repeat("0", d.scale-len(digits)+1) + digits |
| 99 | } |
| 100 | split := len(digits) - d.scale |
| 101 | return sign + digits[:split] + "." + digits[split:] |
| 102 | } |
| 103 | |
| 104 | // StringFixed returns a string representation with exactly places digits |
| 105 | // after the decimal point. Pads with zeros or truncates as needed. |
| 106 | // decSep and thousandsSep control formatting; zero values mean no custom separator. |
| 107 | func (d Decimal) StringFixed(places int, decSep, thousandsSep byte) string { |
| 108 | var sb strings.Builder |
| 109 | sb.Grow(32) |
| 110 | d.WriteFixed(&sb, places, decSep, thousandsSep) |
| 111 | return sb.String() |
| 112 | } |
| 113 | |
| 114 | // WriteFixed writes a string representation with exactly places digits |
| 115 | // after the decimal point directly into sb. Pads with zeros or truncates |
| 116 | // as needed. decSep and thousandsSep control formatting; zero values mean |
| 117 | // no custom separator. |
| 118 | func (d Decimal) WriteFixed(sb *strings.Builder, places int, decSep, thousandsSep byte) { |
| 119 | if d.IsZero() { |
| 120 | sb.WriteByte('0') |
| 121 | if places > 0 { |
| 122 | if decSep != 0 { |
| 123 | sb.WriteByte(decSep) |
| 124 | } else { |
| 125 | sb.WriteByte('.') |
| 126 | } |
| 127 | for range places { |
| 128 | sb.WriteByte('0') |
| 129 | } |
| 130 | } |
| 131 | return |
| 132 | } |
| 133 | |
| 134 | var digitBuf [128]byte |
| 135 | digits := d.coeff.Append(digitBuf[:0], 10) |
| 136 | |
| 137 | sign := false |
| 138 | if len(digits) > 0 && digits[0] == '-' { |
| 139 | sign = true |
| 140 | digits = digits[1:] |
| 141 | } |
| 142 | |
| 143 | intLen := len(digits) - d.scale |
| 144 | |
| 145 | if sign { |
| 146 | sb.WriteByte('-') |
| 147 | } |
| 148 | |
| 149 | // Integer part |
| 150 | if intLen <= 0 { |
| 151 | sb.WriteByte('0') |
| 152 | } else { |
| 153 | for i := range intLen { |
| 154 | if thousandsSep != 0 && i > 0 && (intLen-i)%3 == 0 { |
| 155 | sb.WriteByte(thousandsSep) |
| 156 | } |
| 157 | sb.WriteByte(digits[i]) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Fractional part |
| 162 | if places > 0 { |
| 163 | if decSep != 0 { |
| 164 | sb.WriteByte(decSep) |
| 165 | } else { |
| 166 | sb.WriteByte('.') |
| 167 | } |
| 168 | |
| 169 | written := 0 |
| 170 | if intLen > 0 { |
| 171 | n := min(d.scale, places) |
| 172 | for i := range n { |
| 173 | sb.WriteByte(digits[intLen+i]) |
| 174 | written++ |
| 175 | } |
| 176 | } else { |
| 177 | leadingZeros := -intLen |
| 178 | for ; written < leadingZeros && written < places; written++ { |
| 179 | sb.WriteByte('0') |
| 180 | } |
| 181 | for i := 0; i < len(digits) && written < places; i++ { |
| 182 | sb.WriteByte(digits[i]) |
| 183 | written++ |
| 184 | } |
| 185 | } |
| 186 | for ; written < places; written++ { |
| 187 | sb.WriteByte('0') |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func (d Decimal) Neg() Decimal { |
| 193 | if d.coeff == nil || d.coeff.Sign() == 0 { |
| 194 | return Decimal{} |
| 195 | } |
| 196 | return Decimal{coeff: new(big.Int).Neg(d.coeff), scale: d.scale} |
| 197 | } |
| 198 | |
| 199 | func (d Decimal) Sub(other Decimal) Decimal { return d.Add(other.Neg()) } |
| 200 | func (d Decimal) Add(other Decimal) Decimal { |
| 201 | a, b, scale := align(d, other) |
| 202 | sum := new(big.Int).Add(a, b) |
| 203 | return Decimal{coeff: sum, scale: scale}.normalized() |
| 204 | } |
| 205 | |
| 206 | func (d Decimal) Mul(other Decimal) Decimal { |
| 207 | if d.IsZero() || other.IsZero() { |
| 208 | return Decimal{} |
| 209 | } |
| 210 | product := new(big.Int).Mul(d.coeffOrZero(), other.coeffOrZero()) |
| 211 | return Decimal{coeff: product, scale: d.scale + other.scale}.normalized() |
| 212 | } |
| 213 | |
| 214 | func (d Decimal) Cmp(other Decimal) int { |
| 215 | a, b, _ := align(d, other) |
| 216 | return a.Cmp(b) |
| 217 | } |
| 218 | |
| 219 | func (d Decimal) Equal(other Decimal) bool { |
| 220 | return d.Cmp(other) == 0 |
| 221 | } |
| 222 | |
| 223 | func (d Decimal) IsZero() bool { |
| 224 | return d.coeff == nil || d.coeff.Sign() == 0 |
| 225 | } |
| 226 | |
| 227 | func align(a, b Decimal) (aCoeff *big.Int, bCoeff *big.Int, scale int) { |
| 228 | scale = max(b.scale, a.scale) |
| 229 | |
| 230 | aCoeff = a.coeffOrZero() |
| 231 | bCoeff = b.coeffOrZero() |
| 232 | if delta := scale - a.scale; delta > 0 { |
| 233 | aCoeff.Mul(aCoeff, pow10(delta)) |
| 234 | } |
| 235 | if delta := scale - b.scale; delta > 0 { |
| 236 | bCoeff.Mul(bCoeff, pow10(delta)) |
| 237 | } |
| 238 | return aCoeff, bCoeff, scale |
| 239 | } |
| 240 | |
| 241 | func (d Decimal) coeffOrZero() *big.Int { |
| 242 | if d.coeff == nil { |
| 243 | return new(big.Int) |
| 244 | } |
| 245 | return new(big.Int).Set(d.coeff) |
| 246 | } |
| 247 | |
| 248 | func (d Decimal) normalized() Decimal { |
| 249 | if d.coeff == nil || d.coeff.Sign() == 0 { |
| 250 | return Decimal{} |
| 251 | } |
| 252 | if d.scale == 0 { |
| 253 | return Decimal{coeff: new(big.Int).Set(d.coeff)} |
| 254 | } |
| 255 | |
| 256 | sign := d.coeff.Sign() |
| 257 | abs := new(big.Int).Abs(d.coeff) |
| 258 | ten := big.NewInt(10) |
| 259 | rem := new(big.Int) |
| 260 | for d.scale > 0 { |
| 261 | quotient, _ := new(big.Int).QuoRem(abs, ten, rem) |
| 262 | if rem.Sign() != 0 { |
| 263 | break |
| 264 | } |
| 265 | abs = quotient |
| 266 | d.scale-- |
| 267 | } |
| 268 | |
| 269 | if sign < 0 { |
| 270 | abs.Neg(abs) |
| 271 | } |
| 272 | return Decimal{coeff: abs, scale: d.scale} |
| 273 | } |
| 274 | |
| 275 | func pow10(n int) *big.Int { |
| 276 | if n <= 0 { |
| 277 | return big.NewInt(1) |
| 278 | } |
| 279 | return new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(n)), nil) |
| 280 | } |