]> arthur.barton.de Git - netdata.git/commitdiff
fix float2int()
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 23 Jan 2016 11:51:31 +0000 (13:51 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 23 Jan 2016 11:51:31 +0000 (13:51 +0200)
plugins.d/charts.d.plugin

index 43a96947a41559148d696aec239b89d436b20a06..b3b387f964f2c63faf9c2d8f4744d718fbe596b4 100755 (executable)
@@ -239,32 +239,44 @@ fixid() {
 # to integer, give a multiplier
 # the result is stored in ${FLOAT2INT_RESULT}
 # so that no fork is necessary
+# the multiplier must be a power of 10
 float2int() {
        local f="$1" m="$2" a b l
 
-       # echo >&2 "f='${f}', m='${m}'"
+       #echo >&2 "f='${f}', m='${m}'"
+
+       # the length of the multiplier - 1
+       l=$[ ${#m} - 1 ]
 
        # split the floating point number
        # in integer (a) and decimal (b)
        a=${f/.*/}
        b=${f/*./}
 
-       # the length of the multiplier
-       l=$[ ${#m} - 1 ]
-
-       # cut the decimal part if it is
-       # longer than the multiplier
-       [ ${#b} -gt ${l} ] && b=${b:0:${l}}
-       b=$[10#$b]
-
-       # prepend a zero to the integer
-       # part, if it is missing
+       # prepend a zero to the integer part
+       # if it is missing
        [ -z "${a}" ] && a="0"
+       # strip leading zeros - base 10 convertion
        a=$[10#$a]
 
+       # check the length of the decimal part
+       # against the length of the multiplier
+       if [ ${#b} -gt ${l} ]
+               then
+               # too many digits - take the most significant
+               b=${b:0:${l}}
+       elif [ ${#b} -lt ${l} ]
+               then
+               # too few digits - pad with zero on the right
+               local z="00000000000000000000000" r=$[l - ${#b}]
+               b="${b}${z:0:${r}}"
+       fi
+       # strip leading zeros - base 10 convertion
+       b=$[10#$b]
+
        # store the result
        FLOAT2INT_RESULT=$[ (a * m) + b ]
-       # echo >&2 "FLOAT2INT_RESULT='${FLOAT2INT_RESULT}'"
+       #echo >&2 "FLOAT2INT_RESULT='${FLOAT2INT_RESULT}'"
 }