From 28f70b99f183caa7ebd8f55ea95d9997dd3a9003 Mon Sep 17 00:00:00 2001 From: "Costa Tsaousis (ktsaou)" Date: Sat, 23 Jan 2016 13:51:31 +0200 Subject: [PATCH] fix float2int() --- plugins.d/charts.d.plugin | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/plugins.d/charts.d.plugin b/plugins.d/charts.d.plugin index 43a96947..b3b387f9 100755 --- a/plugins.d/charts.d.plugin +++ b/plugins.d/charts.d.plugin @@ -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}'" } -- 2.39.2