Skip to content
Snippets Groups Projects
Commit 57150384 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

ph.toUnit - added 3rd param

parent 3a6765ea
No related branches found
No related tags found
1 merge request!143netio shows network speed 2 digits after "."
......@@ -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.#"
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment