Need function to add commas to integers  awk
This is a discussion on Need function to add commas to integers  awk ; I have a function that will take a floating point variable and add
commas to it but I have not been able to get it to work with integers.
Any ideas?
# put commas in num
# input: a number
...

Need function to add commas to integers
I have a function that will take a floating point variable and add
commas to it but I have not been able to get it to work with integers.
Any ideas?
# put commas in num
# input: a number
# output: the number with commas
function comma(x, num) {
if (x < 0)
return "" comma(x)
num = sprintf("%.2f", x) # num is dddddd.dd
while (num ~ /[09][09][09][09]/)
sub(/[09][09][09][,.]/, ",&", num)
return num
}

Robert B. Peirce, Venetia, PA 7249416883
bob AT peircefamily.com [Mac]
rbp AT cooksonpeirce.com [Office]

Re: Need function to add commas to integers
On 20051011, Robert Peirce wrote:
> I have a function that will take a floating point variable and add
> commas to it but I have not been able to get it to work with integers.
> Any ideas?
>
> # put commas in num
> # input: a number
> # output: the number with commas
> function comma(x, num) {
> if (x < 0)
> return "" comma(x)
> num = sprintf("%.2f", x) # num is dddddd.dd
> while (num ~ /[09][09][09][09]/)
> sub(/[09][09][09][,.]/, ",&", num)
> return num
> }
function commas(n) {
point = index(n,".")  1
if (point < 0) point = length(n)
while (point > 3) {
point = 3
n = substr(n,1,point)","substr(n,point + 1)
}
return n
}

Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A ProblemSolution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>

Re: Need function to add commas to integers
Robert Peirce wrote:
> I have a function that will take a floating point variable and add
> commas to it but I have not been able to get it to work with integers.
> Any ideas?
>
> # put commas in num
> # input: a number
> # output: the number with commas
> function comma(x, num) {
> if (x < 0)
> return "" comma(x)
> num = sprintf("%.2f", x) # num is dddddd.dd
> while (num ~ /[09][09][09][09]/)
> sub(/[09][09][09][,.]/, ",&", num)
> return num
> }
>
Just change the RE for your sub() command to include an "end of string"
possibility and this will work for integers or floating point numbers:
function comma(x, num) {
if (x < 0)
return "" comma(x)
num = x # num is dddddd or dddd.dd
while (num ~ /[09][09][09][09]/)
sub(/[09][09][09]($[,.])/, ",&", num)
return num
}
ALternatively this will do the same with GNU awk (to get (gensub()"):
function comma (x, num) {
if (x < 0)
return "" comma(x)
num = x
while (num !=
(num=gensub(/([09])([09][09][09])($[,.])/,"\\1,\\2\\3","",num)));
return num
}
Regards,
Ed.

Re: Need function to add commas to integers
Robert Peirce wrote:
> I have a function that will take a floating point variable and add
> commas to it but I have not been able to get it to work with integers.
> Any ideas?
>
> # put commas in num
> # input: a number
> # output: the number with commas
> function comma(x, num) {
> if (x < 0)
> return "" comma(x)
> num = sprintf("%.2f", x) # num is dddddd.dd
> while (num ~ /[09][09][09][09]/)
> sub(/[09][09][09][,.]/, ",&", num)
> return num
> }
function comma( left, right )
{
if ( ""==left ) return right
if ( right ~ /^[09][09][09]/ && left ~ /[09]$/ )
right = "," right
return comma(substr(left,1,length(left)1),
substr(left,length(left)) right )
}

Re: Need function to add commas to integers
Thanks to everybody who suggested a solution. I intend to try each of
them. In the meantime, I played around with this today and finally came
up with my own solution. This may not be as elegant as some that were
suggested, but it works. Here is comma.awk:
# put commas in num and take them out ($ too)
# input: a number
# output: the number with commas
# Floating point
function comma(x, num) {
if (x < 0)
return "" comma(x)
num = sprintf("%.2f", x) # num is dddddd.dd
while (num ~ /[09][09][09][09]/)
sub(/[09][09][09][,.]/, ",&", num)
return num
}
# Integer
function commaI(x, num) {
if (x < 0)
return "" commaI(x)
num = sprintf("%d", x+0.5) # num is dddddd
while (num ~ /[09][09][09][09]/) {
if (num !~ ",")
sub(/[09][09][09]$/, ",&", num)
else
sub(/[09][09][09],/, ",&", num)
}
return num
}
# input: a number
# output: a number without commas or $
function nocomma(num) {
gsub("\\$", "", num)
gsub(",", "", num)
return num
}

Robert B. Peirce, Venetia, PA 7249416883
bob AT peircefamily.com [Mac]
rbp AT cooksonpeirce.com [Office]
Similar Threads

By Application Development in forum Python
Replies: 1
Last Post: 12112007, 06:32 PM

By Application Development in forum awk
Replies: 0
Last Post: 10122005, 07:39 PM