shell脚本nicenumber实现代码
Givenanumber,showsitincomma-separatedform.ExpectsDDandTDtobeinstantiated.Instantiatesnicenum.or,ifasecondargisspecified,theoutputisechoedtostdout.
废话不多说,首先是
#!/bin/sh
#nicenumber--Givenanumber,showsitincomma-separatedform.
#ExpectsDDandTDtobeinstantiated.Instantiatesnicenum
#or,ifasecondargisspecified,theoutputisechoedtostdout.
nicenumber()
{
#Notethatweassumethat'.'isthedecimalseparatorin
#theINPUTvaluetothisscript.Thedecimalseparatorintheoutputvalueis
#'.'unlessspecifiedbytheuserwiththe-dflag
integer=$(echo$1|cut-d.-f1)#leftofthedecimal
decimal=$(echo$1|cut-d.-f2)#rightofthedecimal
if[$decimal!=$1];then
#There'safractionalpart,solet'sincludeit.
result="${DD:="."}$decimal"
fi
thousands=$integer
while[$thousands-gt999];do
remainder=$(($thousands%1000))#threeleastsignificantdigits
while[${#remainder}-lt3];do#forceleadingzerosasneeded
remainder="0$remainder"
done
thousands=$(($thousands/1000))#toleftofremainder,ifany
result="${TD:=","}${remainder}${result}"#buildsrighttoleft
done
nicenum="${thousands}${result}"
if[!-z$2];then
echo$nicenum
fi
}
DD="."#decimalpointdelimiter,toseparateintegerandfractionalvalues
TD=","#thousandsdelimiter,toseparateeverythreedigits
whilegetopts"d:t:"opt;do
case$optin
d)DD="$OPTARG";;
t)TD="$OPTARG";;
esac
done
shift$(($OPTIND-1))
if[$#-eq0];then
echo"Usage:$(basename$0)[-dc][-tc]numericvalue"
echo"-dspecifiesthedecimalpointdelimiter(default'.')"
echo"-tspecifiesthethousandsdelimiter(default',')"
exit0
fi
nicenumber$11#secondargforcesnicenumberto'echo'output
exit0
这脚本我们以后分析,现在先mark下。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下