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 "."
......@@ -37,6 +37,7 @@
# 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