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下。
热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短