如何在bash函数中将数字显示到小数点后两位

如何在bash函数中将数字显示到小数点后两位

问题描述:

我应该如何获取以秒为单位的数字,并以秒为单位显示到小数点后两位?伪代码遵循dTime函数,我不确定,但是您会得到我想要的目标.

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

Bash具有内置的printf函数:

Bash has a printf function built in:

printf "%0.2f\n" $T