如何在bash函数中将数字显示到小数点后两位 [英] How to display number to two decimal places in bash function

查看:101
本文介绍了如何在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

这篇关于如何在bash函数中将数字显示到小数点后两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆