diff options
| author | 2025-05-22 16:27:55 +0200 | |
|---|---|---|
| committer | 2025-05-22 16:27:55 +0200 | |
| commit | b6ff55662e0281c0d6e111f9307625ef695df2fa (patch) | |
| tree | 5f7761efa0b51a7a7d56f96fce3681c8e9b66fe9 /vendor/github.com/tdewolff/parse/v2/strconv/decimal.go | |
| parent | [chore/woodpecker] don't make `test` depend on `lint` (#4189) (diff) | |
| download | gotosocial-b6ff55662e0281c0d6e111f9307625ef695df2fa.tar.xz | |
[chore] update dependencies (#4188)
Update dependencies:
- github.com/gin-gonic/gin v1.10.0 -> v1.10.1
- github.com/gin-contrib/sessions v1.10.3 -> v1.10.4
- github.com/jackc/pgx/v5 v5.7.4 -> v5.7.5
- github.com/minio/minio-go/v7 v7.0.91 -> v7.0.92
- github.com/pquerna/otp v1.4.0 -> v1.5.0
- github.com/tdewolff/minify/v2 v2.23.5 -> v2.23.8
- github.com/yuin/goldmark v1.7.11 -> v1.7.12
- go.opentelemetry.io/otel{,/*} v1.35.0 -> v1.36.0
- modernc.org/sqlite v1.37.0 -> v1.37.1
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4188
Reviewed-by: Daenney <daenney@noreply.codeberg.org>
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/github.com/tdewolff/parse/v2/strconv/decimal.go')
| -rw-r--r-- | vendor/github.com/tdewolff/parse/v2/strconv/decimal.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go b/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go index 788bb9622..137c87936 100644 --- a/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go +++ b/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go @@ -69,3 +69,81 @@ func ParseDecimal(b []byte) (float64, int) { } return f * math.Pow10(exp), i } + +// AppendDecimal appends a float to `b` with `dec` the maximum number of decimals. +func AppendDecimal(b []byte, f float64, dec int) []byte { + if math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) { + return b + } + + if dec < 0 || 17 < dec { + dec = 17 + } + f *= math.Pow10(dec) + + // correct rounding + if 0.0 <= f { + f += 0.5 + } else { + f -= 0.5 + } + + // calculate mantissa and exponent + num := int64(f) + if num == 0 { + return append(b, '0') + } + for 0 < dec && num%10 == 0 { + num /= 10 + dec-- // remove trailing zeros + } + + i, n := len(b), LenInt(num) + if 0 < dec { + if n < dec { + n = dec // number has zero after dot + } + n++ // dot + if lim := int64pow10[dec]; 0 < num && num < lim || num < 0 && -lim < num { + n++ // zero at beginning + } + } + if cap(b) < i+n { + b = append(b, make([]byte, n)...) + } else { + b = b[:i+n] + } + + // print sign + if num < 0 { + num = -num + b[i] = '-' + } + i += n - 1 + + // print number + if 0 < dec { + b[i] = byte(num%10) + '0' + num /= 10 + dec-- + i-- + for 0 < dec { + b[i] = byte(num%10) + '0' + num /= 10 + dec-- + i-- + } + b[i] = '.' + i-- + } + if num == 0 { + b[i] = '0' + } else { + for num != 0 { + b[i] = byte(num%10) + '0' + num /= 10 + i-- + } + } + return b +} |
