From 59b07665ab5a12c2517be2950556d395498c1fda Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 24 Feb 2023 12:03:43 -0500 Subject: [PATCH] Modify float formatting again Make sure at least one decimal is in a float. Maximum precision of 5 past. --- confluent_client/bin/nodesensors | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/confluent_client/bin/nodesensors b/confluent_client/bin/nodesensors index 832a104a..119df18c 100755 --- a/confluent_client/bin/nodesensors +++ b/confluent_client/bin/nodesensors @@ -37,6 +37,12 @@ class hybridcsv(csv.excel): lineterminator = '\n' +def floatformat(num): + fm = u'{:.5f}'.format(num).rstrip('0') + if fm[-1:] == u'.': + return fm + u'0' + return fm + csv.register_dialect('hybrid', hybridcsv) import confluent.client as client @@ -135,7 +141,7 @@ def sensorpass(showout=True, appendtime=False): if sensedata['value'] is None: showval = '' elif isinstance(sensedata['value'], float): - showval = u' {0:.5g} '.format(sensedata['value']) + showval = floatformat(sensedata['value']) else: showval = u' {0} '.format(sensedata['value']) if sensedata['units'] not in (None, u''): @@ -192,7 +198,7 @@ def format_csv(csvwriter, orderedsensors, resdata, showtime=True): else: datum = healthstates if isinstance(datum, float): - datum = '{:.5g}'.format(datum) + datum = floatformat(datum) rowdata.append(datum) except KeyError: rowdata.append('N/A')