fix for parsing scientific notation numbers #211
Costa Tsaousis (ktsaou) committed
Apr 12, 2016 at 20:32 UTC
6062038d26dba5368596b6d3f7e7ed79df44098c
1 file changed
+18
-4
plugins.d/charts.d.plugin
+18
-4
@@ -249,15 +249,26 @@ float2int() {
249
# the length of the multiplier - 1
250
l=$[ ${#m} - 1 ]
251
252
+ # check if the number is in scientific notation
253
+ if [[ ${f} =~ ^[[:space:]](-)?[0-9.]+(e|E)(\+|-)[0-9]+ ]]
254
+ then
255
+ # convert it to decimal
256
+ # unfortunately, this fork cannot be avoided
257
+ # if you know of a way to avoid it, please let me know
258
+ f=$(printf "%0.${l}f" ${f})
259
+ fi
260
+
261
# split the floating point number
262
# in integer (a) and decimal (b)
263
a=${f/.*/}
264
b=${f/*./}
265
257
- # prepend a zero to the integer part
258
- # if it is missing
266
+ # if the integer part is missing
267
+ # set it to zero
268
[ -z "${a}" ] && a="0"
260
- # strip leading zeros - base 10 convertion
269
+
270
+ # strip leading zeros from the integer part
271
+ # base 10 convertion
272
a=$[10#$a]
273
274
# check the length of the decimal part
@@ -266,13 +277,16 @@ float2int() {
277
then
278
# too many digits - take the most significant
279
b=${b:0:${l}}
280
+
281
elif [ ${#b} -lt ${l} ]
282
then
283
# too few digits - pad with zero on the right
284
local z="00000000000000000000000" r=$[l - ${#b}]
285
b="${b}${z:0:${r}}"
286
fi
275
- # strip leading zeros - base 10 convertion
287
+
288
+ # strip leading zeros from the decimal part
289
+ # base 10 convertion
290
b=$[10#$b]
291
292
# store the result