Table of Contents

In this article I will share a sample script to **add thousands separator comma in a number** which can be decimal or a full integer. A common mistake that programmers make is to present the results of calculations to the user without formatting them first. It’s difficult for users to ascertain whether 43245435 goes into the millions without counting from right to left and mentally inserting a comma every three digits. So A 'comma' will be used to add thousands separator.

## Script to add thousands separator

Below is a sample script which can be used to add thousands separator comma in a number. You can modify the script based on your requirement.

[root@node1 ~]# cat /tmp/nicenumber.sh #!/bin/bash # nicenumber--Given a number, shows it in comma-separated form. Expects DD # (decimal point delimiter) and TD (thousands delimiter) 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 '.' integer=$(echo $1 | cut -d. -f1) # Left of the decimal decimal=$(echo $1 | cut -d. -f2) # Right of the decimal # Check if number has more than the integer part. 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 # We need 'remainder' to be three digits. Do we need to add zeros? while [ ${#remainder} -lt 3 ] ; do # Force leading zeros remainder="0$remainder" done result="${TD:=","}${remainder}${result}" # Builds right to left thousands=$(($thousands / 1000)) # To left of remainder, if any done nicenum="${thousands}${result}" if [ ! -z $2 ] ; then echo $nicenum fi } DD="." # Decimal point delimiter, to separate whole and fractional values TD="," # Add thousands separator using (,) to separate every three digits # Input validation if [ $# -eq 0 ] ; then echo "Please provide an integer value" exit 0 fi # BEGIN MAIN SCRIPT # ================= nicenumber $1 1 # Second arg forces nicenumber to 'echo' output. exit 0

## How the script works?

The heart of this script is the while loop within the `nicenumber()`

function, which iteratively keeps removing the three least significant digits from the numeric value stored in the variable thousands and attaches these digits to the pretty version of the number that it’s building up.

The loop then reduces the number stored in thousands and feeds it through the loop again if necessary. Once the `nicenumber()`

function is done, the main script logic starts.

First it parses any options passed to the script with `getopts`

and then finally it calls the `nicenumber()`

function with the last argument the user specified.

## Running the script

Here I have executed the script with a couple of examples to add thousands separator to the provided integers and decimal values.

[root@node1 ~]# /tmp/nicenumber.sh 123456789 123,456,789 [root@node1 ~]# /tmp/nicenumber.sh 1234567890 1,234,567,890 [root@node1 ~]# /tmp/nicenumber.sh 1234567890.456 1,234,567,890.456

Lastly I hope the steps from the article to add thousands separator comma in a number using shell/bash script in Linux was helpful. So, let me know your suggestions and feedback using the comment section.

*References:*

*Wicked Cool Scripts*