Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comma function for Indian number system...

Status
Not open for further replies.

visitnag

Programmer
Sep 21, 2006
15
IN
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.
 
Hi

For integers this should do it for any size :
Code:
[COLOR=orange]function comma[/color][teal]([/teal]n          [teal],[/teal] nn[teal])[/teal]
[teal]{[/teal]
    [b]while[/b] [teal]([/teal]n [teal]!=[/teal] nn [teal]=[/teal] [COLOR=orange]gensub[/color][teal]([/teal][fuchsia]/([[:digit:]])([[:digit:]]{3}$|[[:digit:]]{2},)/[/fuchsia][teal],[/teal] [i][green]"[/green][/i][lime]\\[/lime][i][green]1,[/green][/i][lime]\\[/lime][i][green]2"[/green][/i][teal],[/teal] [purple]1[/purple][teal],[/teal] n[teal])) {[/teal]
        n [teal]=[/teal] nn
    [teal]}[/teal]
 
    [b]return[/b] n
[teal]}[/teal]

For fractional part I did not got the rule.


Feherke.
feherke.github.io
 
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.
 
Hi

There is nothing special. Let us rewrite it abit :
Code:
[COLOR=orange]function comma[/color][teal]([/teal]n          [teal],[/teal] new_n[teal])[/teal]
[teal]{[/teal]
    new_n [teal]=[/teal] [COLOR=orange]gensub[/color][teal]([/teal][fuchsia]/([[:digit:]])([[:digit:]]{3}$|[[:digit:]]{2},)/[/fuchsia][teal],[/teal] [i][green]"[/green][/i][lime]\\[/lime][i][green]1,[/green][/i][lime]\\[/lime][i][green]2"[/green][/i][teal],[/teal] [purple]1[/purple][teal],[/teal] n[teal])[/teal]

    [b]while[/b] [teal]([/teal]n [teal]!=[/teal] new_n[teal]) {[/teal]
        n [teal]=[/teal] new_n

        new_n [teal]=[/teal] [COLOR=orange]gensub[/color][teal]([/teal][fuchsia]/([[:digit:]])([[:digit:]]{3}$|[[:digit:]]{2},)/[/fuchsia][teal],[/teal] [i][green]"[/green][/i][lime]\\[/lime][i][green]1,[/green][/i][lime]\\[/lime][i][green]2"[/green][/i][teal],[/teal] [purple]1[/purple][teal],[/teal] n[teal])[/teal]
    [teal]}[/teal]

    [b]return[/b] n
[teal]}[/teal]
[ul]
[li]We insert each comma one by one in a [tt]while[/tt] loop.[/li]
[li]The loop is repeated as long as another comma is added.[/li]
[li]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.[/li]
[li]Each time the value was modifier, we assign the new_n to n, and repeat the modification attempt to obtain a fresh new_n.[/li]
[li]The regular expression used in [tt]gensub()[/tt] could be written shorter as [tt]/([0-9])([0-9]{3}$|[0-9]{2},)/[/tt] ( or in other languages even [tt]/(\d)(\d{3}$|\d{2},)/[/tt] ).[/li]
[li]The regular expressions are matching the number like this :[ul]
[li][pre]
[COLOR=green #9f9]\1[/color][COLOR=blue #99f]\2[/color]
[green]│[/green][blue]╭┴╮[/blue]
[highlight]123456[/highlight][COLOR=green #9f9]7[/color][COLOR=blue #99f]890[/color]
[green]│[/green][blue]╰┬╯[/blue]
[green]│[/green] [blue]╰────────────╮[/blue]
[green]╭────┴────╮[/green] [blue]╭──────┴──────╮[/blue]
/([COLOR=green #9f9][[:digit:]][/color])([COLOR=blue #99f][[:digit:]]{3}$[/color]|[[:digit:]]{2},)/
[/pre][/li]
[li][pre]
[COLOR=green #9f9]\1[/color][COLOR=blue #99f]\2[/color]
[green]│[/green][blue]╭┴╮[/blue]
[highlight]12[/highlight][COLOR=green #9f9]3[/color][COLOR=blue #99f]45,[/color][highlight]67,890[/highlight]
[green]│[/green][blue]╰┬╯[/blue]
[green]│[/green] [blue]╰────────────────────────────╮[/blue]
[green]╭────┴────╮[/green] [blue]╭──────┴──────╮[/blue]
/([COLOR=green #9f9][[:digit:]][/color])([[:digit:]]{3}$|[COLOR=blue #99f][[:digit:]]{2},[/color])/
[/pre][/li]
[/ul][/li]
[/ul]

Feherke.
feherke.github.io
 
Hi

Thinking again, would be simpler and faster with a [tt]for[/tt] loop and [tt]substr()[/tt] :
Code:
[COLOR=orange]function comma[/color][teal]([/teal]n          [teal],[/teal] i[teal])[/teal]
[teal]{[/teal]
    [b]for[/b] [teal]([/teal]i [teal]=[/teal] [b]index[/b][teal]([/teal]n[teal],[/teal] [i][green]"."[/green][/i][teal]) ?[/teal] [b]index[/b][teal]([/teal]n[teal],[/teal] [i][green]"."[/green][/i][teal])[/teal] - [purple]3[/purple] [teal]:[/teal] [b]length[/b][teal]([/teal]n[teal])[/teal] - [purple]2[/purple][teal];[/teal] i [teal]>[/teal] [purple]1[/purple][teal];[/teal] i -[teal]=[/teal] [purple]2[/purple][teal]) {[/teal]
        n [teal]=[/teal] [b]substr[/b][teal]([/teal]n[teal],[/teal] [purple]0[/purple][teal],[/teal] i - [purple]1[/purple][teal])[/teal] [i][green]","[/green][/i] [b]substr[/b][teal]([/teal]n[teal],[/teal] i[teal])[/teal]
    [teal]}[/teal]

    [b]return[/b] n
[teal]}[/teal]


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top