Script for formatting the computing result

Source: Internet
Author: User

A common mistake for script programmers who format computing results is that they are not formatted as soon as the computing results are displayed to users. If you do not manually count from right to left and insert a comma into each of the three digits in your mind, it is difficult to define whether the number 43245435 has reached one million (Chinese in English). I think it is better to insert a comma into four numbers, if Chinese requires such a count ). Use the following script to format your computing results. 01nicenumber. sh02 #! /Bin/sh03 # nicenumber. sh -- specify a number and separate it with commas (,). 04 # Use DD and TD as instances. Instantiate nicenumber. sh. If the second parameter is specified, a standard output is generated. 05 06 nicenumber () 07 {08 # Note: In the input, it is assumed that the point number is a decimal place. 09 # The decimal separator in the output is the dot, unless the-d option is used. 10 11 integer = $ (echo $1 | cut-d. -f1) #12 decimal =$ (echo $1 | cut-d. -f2) #13 to the right of the decimal point if [$ decimal! = $1]; then15 # Here there is a decimal part that contains 16 result = "$ {DD: = ". "} $ decimal" 17 fi18 19 thousands = $ integer20 21 while [$ thousands-gt 999]; do22 remainder =$ ($ thousands % 1000 )) #3 minimum valid digits 23 24 while [$ {# remainder}-lt 3]; do # If needed, force the header to 0 25 remainder = "0 $ remainder" 26 done27 28 thousands = $ ($ thousands/1000) # number on the left of remainder, if 29 result = "$ {TD: =", "}$ {remainder }$ {result}" # generate 30 done31 32 nicenum = "$ {thousands from right to left} $ {Result} "33 if [! -Z $2]; then34 echo $ nicenum35 fi36} 37 38 DD = ". "# decimal point separator, which distinguishes integer and decimal parts 39 TD =", "# thousands separator, add a 40 41 while getopts" d: t: "opt; do # The following describes the usage of getopts in detail. It is special, but it is very powerful. 42 case $ opt in43 d) DD = "$ OPTARG"; 44 t) TD = "$ OPTARG"; 45 esac46 done47 shift $ ($ OPTIND-1) 48 49 if [$ #-eq 0]; then50 echo "Usage: $ (basename $0) [-d c] numeric value "51 echo"-d specifies the decimal separator (the default is the point number) "52 echo"-t specifies the thousands separator (by default, comma) "53 fi54 55 nicenumber $1 1 # How does the second parameter force nicenumber to output to the standard output 56 57 exit 0 script? The core of this script is the while loop in the nicenumber function. This function is used to obtain the value, divide it into three minimum valid digits (that is, the three digits that appear on the right of the next comma), and the reserved value. Then, the minimum valid bits are processed by the operator. Run the code: to run this script, you only need to specify a very large value, and then the script will either use the default value or specify by flag bit (-d or-t) as needed) to add the decimal separator and the thousands separator. Because the function outputs a number, the result can be as follows: echo "Do you really want pay $ (nicenumber $ price) dollars? "Running result: 01. /nicenumber. sh 589462502 5,894,625 03. /nicenumber. sh 589462532.43304 589,462,532.43305. /nicenumber. sh-d,-t. 589462532.43306 589.462.532, 43307. /newnicenum. sh08 Usage: newnicenum. sh [-d c] [-t c] numeric value09-d specifies the decimal separator (the default is the point number) 10-t specifies the thousands separator (the default is a comma) extended reading: different countries use different decimal separator and thousands separator, so you need to add a flag to flexibly specify. For example, Germany and Italy will use-d ". "And-t", ", France will use-d", "and-t", while Switzerland has four languages, they will use-d ". "And-t "". This example shows how a flexible application is better than writing dead code, so this script tool can be applied to users in most countries. On the other hand, this script writes the decimal point Separator in the input to the point number. If you predict that the decimal point in the input uses different separators, you can change the specified separators in the two cut commands. See the following: integer =$ (echo $1 | cut "-d $ DD"-f1) # decimal =$ (echo $1 | cut "-d $ DD"-f2) on the left of the decimal scale) # Run the command on the right of the decimal scale, but if a completely different decimal separator is used, not that perfect. A more subtle solution is to test before the two lines of code to ensure that the expected decimal separator is required by the user. We can learn the idea in 2nd scripts and use sed to delete all numbers to see what is left: 1 seperator = "$ (echo $1 | sed's /[[: digit:] // G') "2 if [! -Z "$ seperator"-a "$ seperator "! = "$ DD"]; then 3 echo "$0: Unknown decimal seperator $ seperator encounted. "> & 2 4 exit 1 5 fi personal experience: 1. the Separator in cut is the-d option, and-f is the field. Think about awk. This is the simplified version. 2. Reference strings in Shell scripts-quotation marks and braces. 3. getopts: the most powerful tool for analyzing the command line parameters passed to the script. getopts has the following features: 1. all parameters passed to the script must be preceded by a minus sign [-]. getopts will not process parameters without a-prefix. 2. getopts is generally placed in a while loop, and this while loop is somewhat different from the standard while loop, which is not determined by brackets. 3. getopts will replace the description of the getopts structure used by the external command getopt in this script: both d and t are considered as flag options, and d is followed by a colon, it indicates that this option must contain a parameter, that is, t. Shift $ ($ OPTIND-1) is used to move the parameter pointer one bit down. Shift can contain parameters, which are the number of moves. The parameter pointer OPTIND minus 1, that is, pointing to the next parameter. At this time, $1 points to the first non-option parameter. How to understand it, see my test: 1. /newnicenum. sh-d. -t 589462532.345 2 and then print 3 Usage: newnicenum. sh [-d c] [-t c] numeric value4-d specifies the decimal separator (the default is the point number) 5-t specifies the thousands separator (the default is a comma) "why? Because-t is not followed by a parameter, do you still remember the colon after t in getopts? Therefore, 589462532.345 is considered as the t parameter, and $1 points to null, because the numeric parameter is shifted. Therefore, if $ # (number of parameters) is equal to 0, three statements are printed. Test again: 1. /newnicenum. sh-d-t, 589462532.345 2 and then print 3. /newnicenum. sh: Unknown decimal seperator. encounted. why not print the three sentences? Because it considers the decimal point separator as the point number, and $ DD is null ($ DD is assigned in getopts and is empty), the two are different, therefore, the conditions following-a in the if judgment statement starting with the nicenumber function are met. Note: For more information about getopts, see "Advanced Bash-Scripting Guide ". Summarize the extended content. The final script is as follows: 01 #! /Bin/sh02 03 nicenumber () 04 {05 # Note: In the input, it is assumed that the point number is a decimal place. 06 # The decimal separator in the output is the dot, unless the-d option is used. 07 08 seperator = "$ (echo $1 | sed's/[[: digit:] // G')" 09 if [! -Z "$ seperator"-a "$ seperator "! = "$ DD"]; then10 echo "$0: Unknown decimal seperator $ seperator encounted. "> & 211 exit 112 fi13 14 integer =$ (echo $1 | cut"-d $ DD "-f1) # decimal = $ (echo $1 | cut "-d $ DD"-f2) on the left side of the decimal scale #16 17 to the right of the decimal scale if [$ decimal! = $1]; then18 # Here there is a decimal part that contains 19 result = "$ {DD: = ". "} $ decimal" 20 fi21 22 thousands = $ integer23 24 while [$ thousands-gt 999]; do25 remainder =$ ($ thousands % 1000 )) #3 minimum valid digits 26 27 while [$ {# remainder}-lt 3]; do # If needed, force start with 0 28 remainder = "0 $ remainder" 29 done30 31 thousands = $ ($ thousands/1000) # number on the left of remainder, if 32 result = "$ {TD: =", "}$ {remainder }$ {result}" # generate from right to left, if TD has not been set, set its default value to comma 33 Done34 35 nicenum = "$ {thousands }$ {result}" 36 if [! -Z $2]; then37 echo $ nicenum38 fi39} 40 41 DD = ". "# decimal point separator, which distinguishes integer and decimal parts 42 TD =", "# thousands separator, add a 43 44 while getopts" d: t: "opt; do # The following describes the usage of getopts in detail. It is special, but it is very powerful. 45 case $ opt in46 d) DD = "$ OPTARG"; 47 t) TD = "$ OPTARG"; 48 esac49 done50 shift $ ($ OPTIND-1) 51 52 if [$ #-eq 0]; then53 echo "Usage: "$ (basename $0)" [-d c] [-t c] numeric value "54 echo"-d specifies the decimal point separator (the default is the point number) "55 echo"-t specifies the thousands separator (by default, comma) "56 fi57 58 nicenumber $1 1 1 # The second parameter forces nicenumber to output to standard output 59 60 exit 0

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.