Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion prometheus_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def floatToGoString(d):
# We only need to care about positive values for le/quantile.
if d > 0 and dot > 6:
mantissa = f'{s[0]}.{s[1:dot]}{s[dot + 1:]}'.rstrip('0.')
return f'{mantissa}e+0{dot - 1}'
return f'{mantissa}e+{dot - 1:02d}'
return s


Expand Down
18 changes: 18 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest

from prometheus_client.utils import floatToGoString


class TestFloatToGoString(unittest.TestCase):
def test_exponent_two_digits_has_no_leading_zero(self):
# floatToGoString mirrors Go's strconv.FormatFloat(f, 'g', -1, 64),
# which pads the exponent to a minimum of two digits. A two-digit
# exponent must not gain a spurious leading zero.
self.assertEqual('1e+10', floatToGoString(1e10))
self.assertEqual('1e+15', floatToGoString(1e15))
self.assertEqual('1.234567890123e+12', floatToGoString(1234567890123.0))

def test_exponent_one_digit_is_zero_padded(self):
# Single-digit exponents keep the two-digit zero padding.
self.assertEqual('1e+06', floatToGoString(1e6))
self.assertEqual('1.234567e+06', floatToGoString(1234567.0))