How should I take a number that is in hundreths of seconds and display it in seconds to two decimal places? Psuedo code to follow the dTime function I am not sure about but you'll get what I'm aiming for I think.
function time {
echo "$(date +%N)/10000000"
}
function dTime {
echo "($1/100).(${$1:${#1}-3:${#1}-1})"
}
T=$time
sleep 2
T=$dTime T
Below can be done for 2 decimal precision ,
The following divides the output of
date +%N
by1000000000
, rounds the result to two decimal places and assigns the result to the variableT
.If you just want to print the stuff,
If you don't like
bc
and want to usedc
(which is a bit lighter and much funnier to use as it's reverse polish),Notice the difference, with
printf
you'll have the leading0
, not withbc
anddc
. There's another difference betweenprintf
andbc
(ordc
):printf
rounds to the nearest number to two decimal places, whereasbc
(ordc
) rounds correct to two decimal places. If you want this latter behavior and assign to a variableT
the result, you can use, e.g.,or, if you also want the leading
0
:"bc" and "dc" both truncate rather than round, so nowadays I prefer Perl
If "N" has your number of hundreths of seconds in it then:
I was never a Perl supporter but it is now ubiquitous and I have finally been swayed to using it instead of sed/awk etc.
Bash has a printf function built in: