Shortening Numbers

Hi,

I'm using ksh and I'm trying to shorten a column of figures. For example, I'd like to turn:

3456789.9876 to 89.98
567.4956 to 67.49
4669493932.34564 to 32.34

... so the input figure will vary in length, I'm not worried about rounding, I just want the two figures either side of the dp. I probably know how to do it in awk and cut etc I just wondered if there was a more elegant way using say a number format in something like printf.

So far I haven't found a number format that will exclude figures starting from the extreme left (the millions, 100,000s etc). Anyone?

use expr with % operator. n%100 will give you remainder so u'll get two digits.
u can use substr to extract two digits after decimal.

it's easier to use awk/cut. anyway, ur choice :slight_smile:

Thanks, but never used expr before, had a go, but dont really have the syntax, could you give me the whole line?

Did it with awk and cut but it was messy. Cant find a way to awk or cut the two decimal places left of the dp without doing a wc on the string, it only seems to work left to right.

sed with some possibilities:

sed 's/.*\(..\...\).*/\1/'	#  format ##.##

sed 's/.*\(...\...\).*/\1/'	#  format ###.##

sed 's/.*\(....\...\).*/\1/'	#  format ####.##

sed 's/.*\(..\....\).*/\1/'	#  format ##.###

Regards

echo '3456789.9876' | nawk '{match($0, /..[.]../); print substr($0, RSTART, RLENGTH)}'
cat <<EOF | awk '{p=match($0,"\."); print substr($0,p-2,2)"."substr($0,p+1,2)}'
3456789.9876
567.4956
4669493932.34564
EOF

89.98
67.49
32.34

Wow, thanks everyone, plenty to play around with.