主頁(yè) > 知識(shí)庫(kù) > shell腳本nicenumber實(shí)現(xiàn)代碼

shell腳本nicenumber實(shí)現(xiàn)代碼

熱門標(biāo)簽:智能電銷機(jī)器人有用嗎 怎么在百度地圖標(biāo)注公司的位置 商機(jī)地圖標(biāo)注 鶴崗400電話申請(qǐng) 測(cè)繪地圖標(biāo)注名稱 德陽(yáng)400電話申請(qǐng) 天津電話外呼系統(tǒng)排名 外呼電話系統(tǒng)怎么操作 百度地圖標(biāo)注直線距離

Given a number, shows it in comma-separated form.Expects DD and TD to be instantiated. Instantiates nicenum. or, if a second arg is specified, the output is echoed to stdout.

廢話不多說(shuō),首先是

#!/bin/sh
# nicenumber -- Given a number, shows it in comma-separated form.
# Expects DD and TD to be instantiated. Instantiates nicenum
# or, if a second arg is specified, the output is echoed to stdout.

nicenumber()
{
 # Note that we assume that '.' is the decimal separator in
 # the INPUT value to this script. The decimal separator in the output value is
 # '.' unless specified by the user with the -d flag

 integer=$(echo $1 | cut -d. -f1)       # left of the decimal
 decimal=$(echo $1 | cut -d. -f2)       # right of the decimal

 if [ $decimal != $1 ]; then
  # There's a fractional part, so let's include it.
  result="${DD:="."}$decimal"
 fi

 thousands=$integer

 while [ $thousands -gt 999 ]; do
  remainder=$(($thousands % 1000))  # three least significant digits

  while [ ${#remainder} -lt 3 ] ; do # force leading zeros as needed
   remainder="0$remainder"
  done

  thousands=$(($thousands / 1000))  # to left of remainder, if any
  result="${TD:=","}${remainder}${result}"  # builds right to left
 done

 nicenum="${thousands}${result}"
 if [ ! -z $2 ] ; then
  echo $nicenum
 fi
}

DD="." # decimal point delimiter, to separate integer and fractional values
TD="," # thousands delimiter, to separate every three digits

while getopts "d:t:" opt; do
 case $opt in
  d ) DD="$OPTARG"  ;;
  t ) TD="$OPTARG"  ;;
 esac
done
shift $(($OPTIND - 1))

if [ $# -eq 0 ] ; then
 echo "Usage: $(basename $0) [-d c] [-t c] numeric value"
 echo " -d specifies the decimal point delimiter (default '.')"
 echo " -t specifies the thousands delimiter (default ',')"
 exit 0
fi

nicenumber $1 1     # second arg forces nicenumber to 'echo' output

exit 0

這腳本我們以后分析,現(xiàn)在先mark下。

標(biāo)簽:六盤水 百色 鎮(zhèn)江 武漢 滁州 優(yōu)質(zhì)小號(hào) 丹東 自貢

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《shell腳本nicenumber實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  shell,腳本,nicenumber,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《shell腳本nicenumber實(shí)現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于shell腳本nicenumber實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章