Vertical Alignment of numbers

This may be an easy problem but it has me perplexed. I have a series of text fields arranged vertically in a window. Each text field contains an amount, and I want them to have decimal alignment in the display.

Some values are negative: (123.45) and some are positive: 123.45. I want to add a space after the positive numbers so that the decimals will align between positive and negative numbers.

Adding a space to the string does not seem to work (chr(32)). There doesn’t seem to be a way in the format command to add a blank space, unless I am missing something. Excel allows a placeholder for positive numbers to force decimal alignment. How do you do that in Xojo?

This format string works for me “#.00\ ;#.00-;0.00” - note the backslash followed by a space at the end of the positive format. You’ll also want to use a mono spaced font for example courier to get the alignment exactly correct.

You could also consider using a Listbox to display your data. You can set a decimal alignment on columns & cells using that.


Maybe you cal also set the negative value color to Red.

I think I saw that somewhere (and this recalls me my days as an accounter student “He’s in Red”).

Use a monospace font such as Courier.

Tried a mask with a space after the backslash. Tried a monospaced font like Courier also. Neither worked.

It seems Xojo trims the right side of a string. All spaces are removed before displaying text on the screen.
This is true of both Labels and TextFields. Example below: The left column are TextFields with monospaced font Courier; the right are Labels with font StoneSans (which is not a monospaced font but the number characters are monospaced).

For labels you have a couple of options:

  • move the negative number label to align the decimal point
  • add chr(160) to positive numbers

For textfields I can’t think a way to fix it.

This is what I see using the previously suggested format

Is that windows or mac? What Xojo version?

It seems to be only working on Windows, same code on MacOS strips the trailing space :frowning_face:

I would again suggest using a listbox where you can specifiy decimal alignment on your columns.

1 Like

You need to find out if it’s the text field that’s stripping the space, or the format command. Check to see if the rightmost character is a space before sending it to the text field. My suspicion is that it’s the format command doing this, because the format command has always been a bit wonky.

If the problem turns out to be the text field, then a workaround could be to replace the rightmost space with a non-breaking space, Unicode U+00A0.

Should have definitely mentioned at the beginning - running Xojo on a 27 in iMac 5k (Late 2015) with MacOS 11.6.1. Sorry for the oversight.

I tested this (with chr(160)) and it looks ok when the TextField has focus but not when the focus is not on that field (the non-breaking space is removed). Labels work ok. At least on my mac.

This appears to be a bug in the textfield, not the format command. I’ve created a f/b case feedback://showreport?report_id=66567.

The whole issue would disappear if instead of using parenthesis, negative would appear in another color (Red for instance), or with the minus sign : -123.45.

Two days ago :grinning:

So -123.42 (in Red)

I agree with that, Michel, and if you look at my earlier example, I did make the negative values red.

But it still seems to me that the concept of formatting of a number is not “new territory” and while I am always willing to use “workarounds” there shouldn’t be a restriction on how an accounting display of numbers is shown. To an accountant, this is a significant issue which is solved in Excel with a simple underscore [ _ ] as a placeholder. Same should be true in Xojo.

1 Like

I believe the solution would be to use a listbox instead of a textarea. So you can have one number, or the dot, per column.

I do agree in an ideal world, it would be as simple as a spreadsheet. Unfortunately, that is not the case.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.