- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to display number with separator
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 11:42 AM
03-11-2005 11:42 AM
Does anyone know how to display number with comma separator.
Example:
123445000 display 123,445,000
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 12:13 PM
03-11-2005 12:13 PM
Re: how to display number with separator
What software? POSIX shell, Perl, ...?
Mic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 12:15 PM
03-11-2005 12:15 PM
Re: how to display number with separator
I am using POSIX shell but open for perl solution.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:21 PM
03-11-2005 01:21 PM
Re: how to display number with separator
are you looking for something like
echo "1234567" | perl -pe "while (s/(\d)(\d{3}[\,\s])/\1,\2/) {}"
?
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:25 PM
03-11-2005 01:25 PM
Re: how to display number with separator
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 03:41 PM
03-11-2005 03:41 PM
Re: how to display number with separator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 03:43 PM
03-11-2005 03:43 PM
Solution--------------------------------------
#!/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 05:10 PM
03-11-2005 05:10 PM
Re: how to display number with separator
this :-) Life would be soooooo simple..
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 07:54 PM
03-11-2005 07:54 PM
Re: how to display number with separator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2005 04:35 PM
03-12-2005 04:35 PM
Re: how to display number with separator
$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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2005 06:23 AM
03-14-2005 06:23 AM