b63944b5c1
Bumping compile time requirement to go 1.18 allows us to simplify our pointer helper methods.
33 lines
598 B
Go
33 lines
598 B
Go
package api
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// formatFloat converts the floating-point number f to a string,
|
|
// after rounding it to the passed unit.
|
|
//
|
|
// Uses 'f' format (-ddd.dddddd, no exponent), and uses at most
|
|
// maxPrec digits after the decimal point.
|
|
func formatFloat(f float64, maxPrec int) string {
|
|
v := strconv.FormatFloat(f, 'f', -1, 64)
|
|
|
|
idx := strings.LastIndex(v, ".")
|
|
if idx == -1 {
|
|
return v
|
|
}
|
|
|
|
sublen := idx + maxPrec + 1
|
|
if sublen > len(v) {
|
|
sublen = len(v)
|
|
}
|
|
|
|
return v[:sublen]
|
|
}
|
|
|
|
// pointerOf returns a pointer to a.
|
|
func pointerOf[A any](a A) *A {
|
|
return &a
|
|
}
|