Shell Script Nicenumber Implementation code _linux shell

Source: Internet
Author: User
Tags numeric value

Given A number, shows it in comma-separated form. expects DD and TD to is instantiated. Instantiates Nicenum. Or, if a second arg is specified, the output was echoed to stdout.

Nonsense not much to say that the first is

#!/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 was echoed to stdout. Nicenumber () {# Note-we assume that '. ' Are the decimal separator in # The INPUT value to this script. The decimal separator in the output value was # '. ' unless specified by the user with The-d flag integer=$ (echo $ | cu T-d-f1) # Left of the decimal decimal=$ (Echo | cut-d.-f2) # Right of the decimal if [$decimal!= $ 1];
  Then # There ' 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 REM Ainder, if any result= "${td:=", "}${remainder}${result}" #Builds right to left do nicenum= "${thousands}${result}" if [!-Z $];  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 $) [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 # second ARG forces To ' echo ' output exit 0

This script we'll analyze later, Mark now.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.