Operating System - HP-UX
1834955 Members
2142 Online
110071 Solutions
New Discussion

Re: how to display number with separator

 
SOLVED
Go to solution
Ridzuan Zakaria
Frequent Advisor

how to display number with separator

Hi,

Does anyone know how to display number with comma separator.

Example:
123445000 display 123,445,000

Thanks.
quest for perfections
10 REPLIES 10
Mic V.
Esteemed Contributor

Re: how to display number with separator

Hi,

What software? POSIX shell, Perl, ...?

Mic
What kind of a name is 'Wolverine'?
Ridzuan Zakaria
Frequent Advisor

Re: how to display number with separator

Hi,

I am using POSIX shell but open for perl solution.

Thanks.
quest for perfections
Michael Schulte zur Sur
Honored Contributor

Re: how to display number with separator

Hi,

are you looking for something like
echo "1234567" | perl -pe "while (s/(\d)(\d{3}[\,\s])/\1,\2/) {}"
?

greetings,

Michael
Bill Hassell
Honored Contributor

Re: how to display number with separator

There is nothing in the shell languages that will do this simply. Even printf does not have this format. Here is a simple script that will add the commas using the AddCommas function. The echo'ed result will have some leading spaces (a fixed field of 13 chars) but these can be eliminated if necessary:

#!/usr/bin/sh
function AddCommas
{
DIGITS=${#1}
typeset -R10 NUM=$1
CHR1=$(echo "$NUM" | cut -c 1)
CHR234=$(echo "$NUM" | cut -c 2-4)
CHR567=$(echo "$NUM" | cut -c 5-7)
CHR8910=$(echo "$NUM" | cut -c 8-10)
typeset -R13 RESULT

if [ $DIGITS -eq 10 ]
then
RESULT="$CHR1,$CHR234,$CHR567,$CHR8910"
echo "$RESULT"
return
elif [ $DIGITS -gt 6 ]
then
RESULT="$CHR234,$CHR567,$CHR8910"
echo "$RESULT"
return
elif [ $DIGITS -gt 3 ]
then
RESULT="$CHR567,$CHR8910"
echo "$RESULT"
return
else
RESULT="$CHR8910"
echo "$RESULT"
fi
return
}
echo "$(AddCommas $1) =$1"

You can test this with something like:

for MYNUM in 1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567891
0
do
./showcommas $MYNUM
done

where showcommas is the above script.


Bill Hassell, sysadmin
Robert Bennett_3
Respected Contributor

Re: how to display number with separator

you can do this with sed


gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds

"All there is to thinking is seeing something noticeable which makes you see something you weren't noticing which makes you see something that isn't even visible." - Norman Maclean
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: how to display number with separator

Here's a routine adapted from a script in O'reilly's Perl Cookbook. It really illustrates the power of Perl's regular expressions. The function actually inverts the string because it's easier to process decimals (if found) and then does the comma's. It then inverts the string again before outputting.

--------------------------------------

#!/usr/bin/perl -w
use strict;

sub commify
{
my $text = reverse($_[0]);
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return(scalar(reverse($text)));
} # commify

my $i = 0;
while ($i <= $#ARGV)
{
my $s = commify($ARGV[$i]);
printf("%s\n",$s);
++$i;
}
exit(0);
----------------------------------------

Use it like this in a shell script:

V1=1000000.98
S1=$(commas.pl ${V1})
echo "V1 = ${S1}"

V1=199999999
S1=$(commas.pl ${V1})
echo "V1 = ${S1}"

It works equally well with floating point or interger values.

If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: how to display number with separator

Ever thought of using C language for things like
this :-) Life would be soooooo simple..

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: how to display number with separator

How about the following "script" ;-)

Save following script in a file "format" and run as
$ format 123445000

--- ksh script "format" start ---
#!/usr/bin/ksh
cat > /tmp/comma.c << 'EOF'
int main (int argc, char *argv[])
{
int i, j, k;
int siz=strlen(argv[1]);
char format[256];
for (i=siz-1, j=0, k=1; i>=0; i--, k++) {
format[j++] = argv[1][i];
if ((!(k%3)) && (i != 0))
format[j++]=',';
}
for (i=j-1; i>=0; i--)
putchar(format[i]);
putchar('\n');
return 0;
}
EOF
/usr/bin/cc -o /tmp/comma /tmp/comma.c > /dev/null 2>&1
/tmp/comma $1
------- ksh script End----------

- Biswajit
:-)
Hein van den Heuvel
Honored Contributor

Re: how to display number with separator

for grins, an other perl solution:

$z = shift @ARGV;
unshift @x,$y while (length ($y=substr($z,-3,3,"")));
print join(",",@x)."\n";

The above use the 'replacement' argument for substr to remove the substr extracted from the original.

and much like Michael's:

$ perl -e '$_=shift @ARGV; while (s/(\d)(\d{3})(,|$)/\1,\2\3/){}; print "$_\n"' 12345

In the above we look for a sequence of 4 decimal anchored by a comma or 'the end' and we remember the components:
1) a decimal

2) three decimals
3) a comma or 'the end'
If found replace that with that first decimal, a comma, then three, and the anchor (comma or end)

repeat untill no more replaces happen.

Hein.
Ridzuan Zakaria
Frequent Advisor

Re: how to display number with separator

Thanks everyone for your help. I am using Clay's suggestion to display comma's.
quest for perfections