Align numbers at decimal point

E

Enno Middelberg

Hi,


I'm sure someone asked this question before, but I can't find the
solution on the web or in the groups.

I want to print out columns of numbers which are aligned at the decimal
point, eg:

123.45
3.0
65.765486

I can't find how to do that. I use the following command as a
workaround, but it only works when the number of digits after the
decimal point is constant:

print ("%.2f" % reduced_time).rjust(9)


Is there an easy solution?


Thanks,

Enno
 
P

Peter Otten

Enno said:
I want to print out columns of numbers which are aligned at the decimal
point, eg:

123.45
3.0
65.765486

One way to do it:
.... pos = f.find(".")
.... if pos == -1:
.... pos = len(f)
.... dec += 1
.... return "%*s%-*s" % (width-dec, f[:pos], dec, f[pos:])
....
print "\n".join([repr(adjust("%s" % f, 10, 5)) for f in [1, 2.3456,
42.42]])
' 1 '
' 2.3456 '
' 42.42 '

Peter
 
A

Alex Martelli

Enno Middelberg said:
I'm sure someone asked this question before, but I can't find the
solution on the web or in the groups.

I want to print out columns of numbers which are aligned at the decimal
point, eg:

123.45
3.0
65.765486

I can't find how to do that. I use the following command as a
workaround, but it only works when the number of digits after the
decimal point is constant:

print ("%.2f" % reduced_time).rjust(9)


Is there an easy solution?

Well, first of all you must have all the numbers at hand before you can
print even just one of them, of course -- you can't know how much space
you need on the left until you know what number out of them all needs
most such space. So, say the numbers are coming from some iterable
all_numbers, just start by accumulating their formatted forms (without
yet any padding), as well as the space each occupies before the decimal
point (since we do know we'll need that information later):

formatted_numbers = []
space_on_the_left = []
for number in all_numbers:
formatted_string = your_favourite_format % number
formatted_numbers.append(formatted_string)
on_the_left = formatted_string.find('.')
if on_the_left < 0: on_the_left = len(formatted_string)
space_on_the_left.append(on_the_left)

You might make this part a bit more compact with a couple of clever list
comprehensions, but I think it's more readable when written out this
way. If you know every formatted string WILL include a period, you
should change the if statement into an assert (or change the call to the
find method into a call to the index method, and remove the following if
statement -- index will raise if it can't find the needle in the
haystack, while find returns -1 in that case).

After this loop you can easily find out how much space you need on the
left for the number that is longest there...:

left_total = max(space_on_the_left)

and now you do have all the info you need to do the printing:

for s, l in zip(formatted_numbers, space_on_the_left):
padding = ' '*(left_total - l)
print '%s%s' % (padding, s)

Again, you can of course make this a bit more compact, for example by
computing the padding string contextually to the print statement, but I
think it's clearer and more readable to give 'padding' its own name.

In many cases you can simplify things if you know left_total in advance,
for example you know that the '.' (if any) must always fall exactly on
the 21st column of each row you're printing (meaning you know that no
number will have more than 20 digits before the . -- and even if they're
all shorter you still want to leave that amount of space). For such a
case, you need only a single loop and no extra memory...:

for number in all_numbers:
formatted_string = your_favourite_format % number
on_the_left = formatted_string.find('.')
if on_the_left < 0: on_the_left = len(formatted_string)
padding = ' '*(left_total - on_the_left)
print '%s%s' % (padding, formatted_string)

(once again, you can trade off legibility for compactness, if you wish).


Alex
 
M

Michael Geary

Enno said:
I want to print out columns of numbers which are aligned at
the decimal point, eg:

123.45
3.0
65.765486

Those decimal points are not aligned on my display.

You can probably guess why: I'm using a proportional font, and spaces are
not as wide as digits.

So, in addition to the algorithmic tips you've gotten, you need to be sure
you're printing in a monospaced font such as Courier.

If you print in a proportional font, you need to use other means to align
the decimal points instead of padding with spaces.

-Mike
 
E

Enno Middelberg

Peter, Alex, and Michael:


thanks for your suggestions and comments. I'll pick one of your
solutions depending on my needs.

Regards,

Enno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,530
Latest member
jameswilliam1

Latest Threads

Top