change number into dollars and cents

in ksh.

Here is what I have working thanks to all of you. Now I need to take it one step further. The $result value needs to be in the $0000.00 format instead of what it is now, 000000. Yes, it is always 6 characters, I thought this would help but so far it has baffled me.

My thought was to pull the right two numbers into one variable and left 4 numbers into another, then concat them with a dollar sign in front. But I cannot get it to work right.

This is what I have that works right now:

 num=`grep "^X1" <filename|cut -c26-31`
 result=`echo $num | awk '$1=$1' OFS="+" | bc`
 
echo "Total Dollar amount is $result"

Forget the last code, try this instead:

result=`echo "$result" | sed 's/^/$/;s/..$/.&/'`
1 Like

Thanks for the printf, never used it before. The result is not what I need and I am guessing I just can't get it to format out as I want. the number is 23240 for example and it is printing out on the screen as 23240.00 instead of $232.40

---------- Post updated at 09:58 AM ---------- Previous update was at 09:41 AM ----------

I have always been hesitant to use the sed command, it has been a while since I was in a UNIX shop, been in windows for the last 8 years and now back in UNIX.

But the sed command is working. Thanks.

Glad it worked :wink: