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 ...

+ Reply to Thread
Results 1 to 5 of 5

Need function to add commas to integers

  1. Default 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 ~ /[0-9][0-9][0-9][0-9]/)
    sub(/[0-9][0-9][0-9][,.]/, ",&", num)
    return num
    }

    --
    Robert B. Peirce, Venetia, PA 724-941-6883
    bob AT peirce-family.com [Mac]
    rbp AT cooksonpeirce.com [Office]


  2. Default Re: Need function to add commas to integers

    On 2005-10-11, 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 ~ /[0-9][0-9][0-9][0-9]/)
    > sub(/[0-9][0-9][0-9][,.]/, ",&", 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 Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

  3. Default 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 ~ /[0-9][0-9][0-9][0-9]/)
    > sub(/[0-9][0-9][0-9][,.]/, ",&", 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 ~ /[0-9][0-9][0-9][0-9]/)
    sub(/[0-9][0-9][0-9]($|[,.])/, ",&", 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(/([0-9])([0-9][0-9][0-9])($|[,.])/,"\\1,\\2\\3","",num)));
    return num
    }

    Regards,

    Ed.

  4. Default 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 ~ /[0-9][0-9][0-9][0-9]/)
    > sub(/[0-9][0-9][0-9][,.]/, ",&", num)
    > return num
    > }



    function comma( left, right )
    {
    if ( ""==left ) return right
    if ( right ~ /^[0-9][0-9][0-9]/ && left ~ /[0-9]$/ )
    right = "," right
    return comma(substr(left,1,length(left)-1),
    substr(left,length(left)) right )
    }


  5. Default 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 ~ /[0-9][0-9][0-9][0-9]/)
    sub(/[0-9][0-9][0-9][,.]/, ",&", 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 ~ /[0-9][0-9][0-9][0-9]/) {
    if (num !~ ",")
    sub(/[0-9][0-9][0-9]$/, ",&", num)
    else
    sub(/[0-9][0-9][0-9],/, ",&", 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 724-941-6883
    bob AT peirce-family.com [Mac]
    rbp AT cooksonpeirce.com [Office]


+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 12-11-2007, 06:32 PM
  2. RE: Need a function to add commas to integers
    By Application Development in forum awk
    Replies: 0
    Last Post: 10-12-2005, 07:39 PM