×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# comma function for Indian number system...

## comma function for Indian number system...

(OP)
Hi

Is there any shorter way to use a comma separator for Indian Number system?
its like ##,##,##,##,###.##. I have written the following code but its quite lengthy...
function comma(n){
if(length(n) <= 3 ){ n = n}else
if(length(n) == 4 ){ n = substr(n,1,1)","substr(n,2)}else
if(length(n) == 5 ){ n = substr(n,1,2)","substr(n,3)}else
if(length(n) == 6 ){ n = substr(n,1,1)","substr(n,2,2)","substr(n,4)}else
if(length(n) == 7 ){ n = substr(n,1,2)","substr(n,3,2)","substr(n,5)}else
if(length(n) == 8 ){ n = substr(n,1,1)","substr(n,2,2)","substr(n,4,2)","substr(n,6)}else
if(length(n) == 9 ){ n = substr(n,1,2)","substr(n,3,2)","substr(n,5,2)","substr(n,7)}else
if(length(n) == 10){ n = substr(n,1,1)","substr(n,2,2)","substr(n,4,2)","substr(n,6,2)","substr(n,8)}else
if(length(n) == 11){ n = substr(n,1,2)","substr(n,3,2)","substr(n,5,2)","substr(n,7,2)","substr(n,9)}else
if(length(n) == 12){ n = substr(n,1,1)","substr(n,2,2)","substr(n,4,2)","substr(n,6,2)","substr(n,8,2)","substr(n,10)}else
if(length(n) == 13){ n = substr(n,1,2)","substr(n,3,2)","substr(n,5,2)","substr(n,7,2)","substr(n,9,2)","substr(n,11)}else
n=n
return(n)

Thank you.

### RE: comma function for Indian number system...

Hi

For integers this should do it for any size :

#### CODE --> Awk

function comma(n          , nn)
{
while (n != nn = gensub(/([[:digit:]])([[:digit:]]{3}$|[[:digit:]]{2},)/, "\\1,\\2", 1, n)) { n = nn } return n }  For fractional part I did not got the rule. Feherke. feherke.github.io ### RE: comma function for Indian number system... (OP) Thank you very much...but...sorry! could you please explain the syntax. I have gone through string manipulation functions in gnu awk manual, but could not understand your syntax. ### RE: comma function for Indian number system... Hi There is nothing special. Let us rewrite it abit : #### CODE --> Awk function comma(n , new_n) { new_n = gensub(/([[:digit:]])([[:digit:]]{3}$|[[:digit:]]{2},)/, "\\1,\\2", 1, n)

while (n != new_n) {
n = new_n

new_n = gensub(/([[:digit:]])([[:digit:]]{3}$|[[:digit:]]{2},)/, "\\1,\\2", 1, n) } return n }  • We insert each comma one by one in a while loop. • The loop is repeated as long as another comma is added. • To know when no comma was added so is time to stop, we store the result of the modification attempt in a local variable new_n. • Each time the value was modifier, we assign the new_n to n, and repeat the modification attempt to obtain a fresh new_n. • The regular expression used in gensub() could be written shorter as /([0-9])([0-9]{3}$|[0-9]{2},)/ ( or in other languages even /(\d)(\d{3}$|\d{2},)/ ). • The regular expressions are matching the number like this : •  \1\2 │╭┴╮ 1234567890 │╰┬╯ │ ╰────────────╮ ╭────┴────╮ ╭──────┴──────╮ /([[:digit:]])([[:digit:]]{3}$|[[:digit:]]{2},)/

•       \1\2
│╭┴╮
12345,67,890
│╰┬╯
│ ╰────────────────────────────╮
╭────┴────╮                  ╭──────┴──────╮
/([[:digit:]])([[:digit:]]{3}\$|[[:digit:]]{2},)/


Feherke.
feherke.github.io

### RE: comma function for Indian number system...

Hi

Thinking again, would be simpler and faster with a for loop and substr() :

#### CODE --> Awk

function comma(n          , i)
{
for (i = index(n, ".") ? index(n, ".") - 3 : length(n) - 2; i > 1; i -= 2) {
n = substr(n, 0, i - 1) "," substr(n, i)
}

return n
} 

Feherke.
feherke.github.io

### RE: comma function for Indian number system...

(OP)
Thank you so much.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!