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

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

For integers this should do it for any size :

## CODE --> Awk

For fractional part I did not got the rule.

Feherke.

feherke.github.io

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

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

There is nothing special. Let us rewrite it abit :

## CODE --> Awk

Feherke.

feherke.github.io

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

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

## CODE --> Awk

Feherke.

feherke.github.io

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