472,989 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Align numbers at decimal point

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

--
Remove "REMOVE_ME" for PM replies
Jul 18 '05 #1
4 8836
Enno Middelberg wrote:
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:
def adjust(f, width, dec): .... 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

Jul 18 '05 #2
Enno Middelberg <en**********************@csiro.au> wrote:
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
Jul 18 '05 #3
Enno Middelberg wrote:
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
Jul 18 '05 #4
Peter, Alex, and Michael:
thanks for your suggestions and comments. I'll pick one of your
solutions depending on my needs.

Regards,

Enno
--
Remove "REMOVE_ME" for PM replies
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stan Brown | last post by:
http://www.acad.sunytccc.edu/instruct/sbrown/stat04/qz05_.htm A table comes near the beginning of the page. I have the <td> of row 1 column 1 styled in line with border-top:none and...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
8
by: janice | last post by:
Hi all, I read a lot of info on this issue, but still don't get it. Why does this happen? document.write(".05" + " \+ " + ".05" + " \= " + (.05 + .05) + "<br>"); document.write("1.05" + "...
6
by: Dennis | last post by:
I am trying to align a column in DataGrid so the decimal's match up (not using fixed fonts). Is there anyone who has code for doing this using StringFormat or something else. Thanks. -- Dennis...
45
by: bobalong | last post by:
Hi I'm have some problem understanding how JS numbers are represented internally. Take this code for an example of weirdness: var biggest = Number.MAX_VALUE; var smaller = Number.MAX_VALUE...
5
by: kj | last post by:
I've tried *many* variations of the following: <TABLE> <TR><TD STYLE='text-align: "."; font-family: Courier'>0.12</TD></TR> <TR><TD STYLE='text-align: "."; font-family: Courier'>10.1</TD></TR>...
7
by: Gerard Flanagan | last post by:
All would anyone happen to have code to generate Cutter Numbers: eg. http://www1.kfupm.edu.sa/library/cod-web/Cutter-numbers.htm or is anyone looking for something to do?-) (I'm under...
1
by: Shailja | last post by:
Hi, How to do alignment of decimal point in Crystal Report? When I open the crystal report, numbers display in following manner: 100.16 10.16 Instead, if second number consists of 2 digits...
1
by: Twanne | last post by:
Hi, I've got some code in VBA where I calculate some numbers. Now some of them are large decimal numbers like -6,3216324E03. So in scientific notations. Now I need to add them to a table with an...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.