Reputation: 62244
In a script, I store the accumulated time in ms from executing several commands in a variable, lets call it total_elapsed_Time
. This contains e.g. values like
1001
or
12
Now, what I want is to output this value in the following form (the time in s with training milliseconds)
1.001
for the first value,
0.012
for the second value. At the moment, I do it like this
printf '%d.%03d s' "$((total_elapsed_time / 1000))" "$((total_elapsed_time % 1000))"
This is okay, and seems to work but I wonder if there is no easier way (so that I only needs to pass the variable to printf and printf takes care of formatting the number).
Upvotes: 0
Views: 50
Reputation: 242008
for x in 1001 12 1 10 100 1000 10000 ; do
x=00$x # Prepend two zeros, so we can safely
x=${x:0: -3}.${x: -3} # insert the dot on the third position from right.
printf '%.3f\n' $x
done
Upvotes: 2