From 5715038450a53123a2246413933c381d59e0e5ba Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 24 Aug 2023 17:09:48 +0200
Subject: [PATCH] ph.toUnit - added 3rd param

---
 inc_pluginfunctions | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/inc_pluginfunctions b/inc_pluginfunctions
index 46f86fb..39f5838 100644
--- a/inc_pluginfunctions
+++ b/inc_pluginfunctions
@@ -27,16 +27,17 @@
 # ----------------------------------------------------------------------
 # 2016-09-23  added getOS
 # 2019-10-29  added setExitcode
-# 2020-03-05  v1.2  <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
-# 2020-09-01  v1.3  <axel.hahn@iml.unibe.ch> added ph.hasParamoption
-# 2022-08-31  v1.4  <axel.hahn@iml.unibe.ch> shellfix corrections
-# 2022-10-25  v1.5  <axel.hahn@iml.unibe.ch> handle empty value in ph.perfadd
-# 2023-01-30  v1.6  <axel.hahn@unibe.ch>     check performance params 5+6 and show a warning if missing
-# 2023-02-16  v1.7  <axel.hahn@unibe.ch>     adding a generic min and max value did not really help
-# 2023-04-24  v1.8  <axel.hahn@unibe.ch>     fix unit conversion
-# 2023-05-05  v1.9  <axel.hahn@unibe.ch>     user specific counter directory
-# 2023-05-17  v1.10 <axel.hahn@unibe.ch>     ph.getOS searches in os-release first
-# 2023-06-22  v1.11 <axel.hahn@unibe.ch>     fix ph.toUnit with float values; shell fixes
+# 2020-03-05  v1.2  <axel.hahn@iml.unibe.ch>  switch to ph.* helper functions
+# 2020-09-01  v1.3  <axel.hahn@iml.unibe.ch>  added ph.hasParamoption
+# 2022-08-31  v1.4  <axel.hahn@iml.unibe.ch>  shellfix corrections
+# 2022-10-25  v1.5  <axel.hahn@iml.unibe.ch>  handle empty value in ph.perfadd
+# 2023-01-30  v1.6  <axel.hahn@unibe.ch>      check performance params 5+6 and show a warning if missing
+# 2023-02-16  v1.7  <axel.hahn@unibe.ch>      adding a generic min and max value did not really help
+# 2023-04-24  v1.8  <axel.hahn@unibe.ch>      fix unit conversion
+# 2023-05-05  v1.9  <axel.hahn@unibe.ch>      user specific counter directory
+# 2023-05-17  v1.10 <axel.hahn@unibe.ch>      ph.getOS searches in os-release first
+# 2023-06-22  v1.11 <axel.hahn@unibe.ch>      fix ph.toUnit with float values; shell fixes
+# 2023-08-24  v1.12 <axel.hahn@unibe.ch>      toUnit got 3rd param for count of digits after "."
 # ======================================================================
 
 
@@ -293,15 +294,24 @@ function ph._getExp(){
 #
 # param  string  value with optional single letter for unit, i.e. 12M
 # param  char    target unit
+# param  integer count of digits after "."; default: none (=integer value)
 function ph.toUnit(){
     local _value=$1
     local _unit=$2
+    local _digits=${3:-0}
 
     local _multiply; _multiply=$( ph._getExp "$_value" )
     local _divisor;  _divisor=$(  ph._getExp "$_unit"  )
 
-    # echo "DEBUG ... $_divisor .. $_multiply"
-    echo "$(echo "$_value" | tr -d "[:alpha:]" )*${_multiply}/$_divisor" | bc
+    local _bc
+    _bc="bc"
+
+    local _dots
+
+    test $_digits -gt 0 && _dots=$( yes "." | head -$_digits | tr -d "\n" )
+    test $_digits -gt 0 && _bc+=" -l | grep -o '.*\\.${_dots}'"
+
+    echo "$(echo "$_value" | tr -d "[:alpha:]" )*${_multiply}/$_divisor" | eval "$_bc" | sed "s#^\.#0.#"
     
 }
 
-- 
GitLab