FormatOpenMetricsFloat works like the usual Go string formatting of a float but appends ".0" if the resulting number would otherwise contain neither a "." nor an "e".
(f float64)
| 33 | // but appends ".0" if the resulting number would otherwise contain neither a |
| 34 | // "." nor an "e". |
| 35 | func FormatOpenMetricsFloat(f float64) string { |
| 36 | // A few common cases hardcoded. |
| 37 | switch { |
| 38 | case f == 1: |
| 39 | return "1.0" |
| 40 | case f == 0: |
| 41 | return "0.0" |
| 42 | case f == -1: |
| 43 | return "-1.0" |
| 44 | case math.IsNaN(f): |
| 45 | return "NaN" |
| 46 | case math.IsInf(f, +1): |
| 47 | return "+Inf" |
| 48 | case math.IsInf(f, -1): |
| 49 | return "-Inf" |
| 50 | } |
| 51 | bp := floatFormatBufPool.Get().(*[]byte) |
| 52 | defer floatFormatBufPool.Put(bp) |
| 53 | |
| 54 | *bp = strconv.AppendFloat((*bp)[:0], f, 'g', -1, 64) |
| 55 | if bytes.ContainsAny(*bp, "e.") { |
| 56 | return string(*bp) |
| 57 | } |
| 58 | *bp = append(*bp, '.', '0') |
| 59 | return string(*bp) |
| 60 | } |
no test coverage detected
searching dependent graphs…