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下。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短