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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to ADD commas to a number string 2

Status
Not open for further replies.

denisl

Technical User
Jun 18, 2001
36
US
I'm trying to convert a number string without commas to have commas so it can be read easily (in Korn/Posix). Sorry, I'm Perl dumb.

For example;

123456789.15 I want to look like this 123,456,789.15

Thanks!
 

Maybe in awk you could write your own function. [ponder]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
uh,

dont ask me how I have done this , but works :)

in PD KSH v5.2.14

Code:
IFS=.
A=1234567.1234
B=123456789123.1234
C=123456789.15
function format_nr {
INT=$1
DEC=$2
RESULT=''
while [[ -n $INT ]]
do

        typeset -R3 INT3=$INT
        RESULT="${INT3},${RESULT}"
        INT=${INT%%(???|??|?)}
done
        echo ${RESULT%%,}.$2
}

format_nr $A
format_nr $B
format_nr $C

Regards,
Marcel

___
____
 
Thanks Marcel.. Tried it and it works!

I just had to add sed 's/ /./g' to put the decimal point back.
 
Please, don't a an unneeded [tt]sed[/tt]. Changing...
Code:
echo ${RESULT%%,}.$2
...to...
Code:
echo "${RESULT%,}.${DEC}"
...will do it.

Hope this helps.
 
Hi,
version 0.2 realesed :)

if you pass an integer '1234', will add two decimals '.00'

Code:
IFS=.
A=1234567.1234
X=1234567
B=123456789123.1234
C=123456789.15
function format_nr {
INT=$1
DEC=$2

#if no decimals, add '.00'
if [[ -z "$DEC" ]]
then
        DEC='00'
fi


RESULT=''
while [[ -n $INT ]]
do

        typeset -R3 INT3=$INT
        RESULT="${INT3},${RESULT}"
        INT=${INT%%(???|??|?)}
done
        echo "${RESULT%%,}.$DEC"
}

format_nr $B
format_nr $C
format_nr $X

___
____
 
Some of the numbers I am processing shouldn't have ".00" at the end.. so v0.2.1

Code:
IFS=.
A=1234567.1234
X=1234567
B=123456789123.1234
C=123456789.15
function format_nr {
INT=$1
DEC=$2

#if no decimals, add '.00'
#if [[ -z "$DEC" ]]
#then
#        DEC='00'
#fi


RESULT=''
while [[ -n $INT ]]
do

        typeset -R3 INT3=$INT
        RESULT="${INT3},${RESULT}"
        INT=${INT%%(???|??|?)}
done

if [[ -z "$DEC" ]]; then
        echo "${RESULT%%,}"
else
        echo "${RESULT%%,}.$DEC"
fi

}

format_nr $A
format_nr $B
format_nr $C
format_nr $X
$ comma.sh
1,234,567.1234
123,456,789,123.1234
123,456,789.15
1,234,567

Now.. can I easily left justify? awk '{print $1}' after the function calls or something better?
 
Not need of awk.
Just pure KSH :)


Code:
IFS=.
X=1234567
B=123456789123.1234
C=123456789.15
function format_nr {
INT=$1
DEC=$2
#if [[ -z "$DEC" ]]
#then
#       DEC='00'
#fi

RESULT=''
while [[ -n $INT ]]
do

        typeset -R3 INT3=$INT
        RESULT="${INT3},${RESULT}"
        INT=${INT%%(???|??|?)}
done


if [[ -z "$DEC" ]]; then
        RESULT="${RESULT%%,}"
else
        RESULT="${RESULT%%,}.$DEC"
fi
echo "${RESULT##(   |  | )}"
}

format_nr $X
format_nr $B
format_nr $C

ksh, is really fun.

PM

___
____
 
Oooh.. that's nice!

Thanks - couldn't ask for anything more!
 
Yes, ksh is fun!

Oh come on, we can tighten this up a bit!
Code:
#!/bin/ksh

A=1234567
B=123456789123.1234
C=123456789.15
D=.1234

function format_nr {
INT=${1%.*}
DEC=${1#${INT}}
RESULT=""

while [[ -n $INT ]]
do
        typeset -R3 INT3=$INT
        RESULT="${INT3},${RESULT}"
        INT=${INT%%(???|??|?)}
done

print ${RESULT%,}${DEC}
}

format_nr $A
format_nr $B
format_nr $C
format_nr $D
That's with or without decimal. We no longer have to set IFS to a dot which can screw up other things a some scripts. The output is left justified. And it's ticghter code. :-D

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top