Home > Archive > AWK > November 2007 > Another number fomatting question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Another number fomatting question
|
|
| Hermann Peifer 2007-11-30, 6:58 pm |
| Hello,
I was wondering if there is an easy way to align numbers with varying
precision at the decimal point.
Examples:
111.1111
2.222222
-33333.3
44444444
....which should look like:
111.1111
2.222222
-33333.3
44444444
(I don't want to add trailing 0's, but rather leave the numbers as they
are.)
Thanks in advance, Hermann
| |
| Kenny McCormack 2007-11-30, 6:58 pm |
| In article <47505D8B.1060105@gmx.net>, Hermann Peifer <peifer@gmx.net> wrote:
>Hello,
>
>I was wondering if there is an easy way to align numbers with varying
>precision at the decimal point.
>
>Examples:
>
>111.1111
>2.222222
>-33333.3
>44444444
>
>...which should look like:
>
> 111.1111
> 2.222222
> -33333.3
>44444444
>
>(I don't want to add trailing 0's, but rather leave the numbers as they
>are.)
>
>Thanks in advance, Hermann
Here's a brute force approach (I put your list of numbers into a file,
fed as input to this [GAWK] script):
{split($1,T,".");printf("%10d%s\n",T[1],2 in T ? "."T[2] : "")}
Note that the "10" is a parameter...
| |
| Hermann Peifer 2007-11-30, 6:58 pm |
| Kenny McCormack wrote:
> In article <47505D8B.1060105@gmx.net>, Hermann Peifer <peifer@gmx.net> wrote:
>
> Here's a brute force approach (I put your list of numbers into a file,
> fed as input to this [GAWK] script):
>
> {split($1,T,".");printf("%10d%s\n",T[1],2 in T ? "."T[2] : "")}
>
> Note that the "10" is a parameter...
>
Thanks. This works fine. I now see that I will have to change the first
format to "%10s" in order to avoid that occasionally appearing string
values like "NA" are printed as "0".
By the way: Is there a difference in using the printf function with and
without round brackets, in GAWK?
Hermann
| |
| Kenny McCormack 2007-11-30, 6:58 pm |
| In article <475074B5.4060704@gmx.net>, Hermann Peifer <peifer@gmx.net> wrote:
....
>By the way: Is there a difference in using the printf function with and
>without round brackets, in GAWK?
Usually not. There are a few obscure cases in which the parens are
necessary, so that's as good a reason as any to always use them. I also
think it looks better with them - one might say it is more "C-like"
(which might be seen as a good thing or not) - so I just always use them.
|
|
|
|
|