
Here's An Easy One for You(Obviously I'm too Stupid)
:> > I want to take the following string - 12345600
:> > and make it 123456.00.
:>
:> this procedure will convert any number string from xxxyy to xxx.yy
:> xxxxyy to xxxx.yy
:> thekeylen = length($4);
:> svalue = substr($4, 1, thekeylen -2) "." substr($4, thekeylen -1, 2);
:> printf "%s", svalue;
: How do you convert the value 123 to 1.23? You divide by 100.
: Simple arithmetic.
: The beauty of awk is that you can do arithmetic with "strings", thus:
This can be very misleading, while it sometimes looks like you can,
if you want your code to be reliable, you really should investigate
how awk really does this.
The short version is that it does "magic" typing, whereby if you treat
it like a number, it will be a number, if like a string, it's a string.
"a"+10 = 10
"a"+"b" = 0
"10"+10 = 20
"10"+10 = 20
etc, etc
: $ awk 'BEGIN { printf("%.2f\n", "12345600" / 100); exit }'
: 123456.00
: $ awk 'BEGIN { printf("%.2f\n", 12345600 / 100); exit }'
: 123456.00
: $
: Same-o, same-o!
: --
: Jim Monty
: Tempe, Arizona USA
--
Ian Stirling. Designing a linux PDA, see http://www.mauve.demon.co.uk/
----- ******* If replying by email, check notices in header ******* -----
Lord, grant me the serenity to accept that I cannot change, the
courage to change what I can, and the wisdom to hide the bodies
of those I had to kill because they pissed me off. Random