Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : right-justifying fields on display
Page 1 of 2 | |||||
Author | Message | ||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
A niggling small problem displaying numbers Sometime it is good eg in tables and so on, to align numeric data vertically ie so either the decimal points line up or the numbers are right-justified. And with C etc formatted outputs, a number can be printed as right-justified and the rest of the field is filled with preceding spaces. BUT when using proportional fonts for a display, the preceding space characters often do not have the same width as a ordinary digit...so the number then displays out of alignment with other numbers above and below it and it looks tacky. Alternatively a regular font can be used but it looks antique ! Maybe if the preceding spaces are replaced with zeros and printed as background... pretty clunky though. Any ideas/suggestions ? Maybe LVGL has a way, haven't looked into it though. |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6781 |
I can't think of a way in which you could do it in a proportional font. Maybe by printing all the numbers as strings, but the width of a space might be a problem. Lining up the decimal points would probably require printing a number as two strings. Nope, not something I'd bother about. :) Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Martin H. Guru Joined: 04/06/2022 Location: GermanyPosts: 1113 |
try this ?number$(123,5) 'converts a number to a digit% long string with leading zeros Function number$(va%,digit%) Local integer l number$=Str$(va%) l=Len(number$) If l<digit% Then number$=String$(digit%-l,48)+number$ End Function Edited 2024-08-04 01:24 by Martin H. 'no comment |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3150 |
A hard problem. Typically, in traditional typesetting, even in a proportional font, numerals would have the same width, and the problem would be with the spaces. Fixed width spaces would be available--"em" space and "en" space, where the "en" space would be the same width as a number, and the "em" space would be twice that. What proportional font do you have, and what display? MMBasic works with fixed-width fonts. I had a lot of fun in the early eighties writing fully justified (right and left) text using proportional fonts on the first commercially available Xerox laser printer--counting the width of each letter to determine what would fit in a line, and then before printing, going back and adjusting the width of the spaces so that most would be the same width and the remainder would be one pixel shorter. That's similar to your problem, but if your font has em and en spaces, your task would be easier. If your display is html, then you have en space : or and em space : or . (Oops, those en and em space strings got translated: ) ~ Edited 2024-08-04 01:36 by lizby PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
I don't think I have ever encountered a font where the numerals were diffent widths or kerned - I suspect precisely for the problem you are experiencing now for fixed width numerals, STR$() is your buddy. STR$(number, number of leading digits, number of trailing digits) STR$(100,3,2) = "100.00" STR$(1,3,2) = " 1.00" STR$(49.72,3,2)= " 49.72" with all the dots nicely lined up if you are putting them in "HTML", put them in a <TD align="right"> tag (you have to build the table too) Edited 2024-08-04 03:53 by CaptainBoing |
||||
EDNEDN Senior Member Joined: 18/02/2023 Location: United StatesPosts: 118 |
Other full featured BASIC's have a "Print Using" to help with formatting output. That doesn't really help with the proportional font issues, but that would be a nice feature to have in MMBasic. QBasic's "Print Using" is documented here: https://qbasic.com/documentation/PRINT-USING.html |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6781 |
Unfortunately PRINT USING is (I believe) a large, clunky and slow chunk of code that not everyone needs - and that most, in fact, virtually never use. You tend to find it in disk BASICs because the interpreter can be a lot bigger than ROM versions. It isn't really needed when you can use MID$() to change parts of a string. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
PRINT USING is a good example of a great concept badly implimented - a "stove-pipe" statement - you can only do one thing with it and what goes in the bottom comes out the top - you can't intercept it for other things. All you can do is ... print it... not terribly flexible. Never presume how a primitive will get used - programmers are a sneaky lot. I think older versions of MMBasic (and maybe larger versions) had/have the "FORMAT$()" function which does pretty much the same as PRINT USING but returns the resultant output for you to do with as you please. It is a lot of work to interpret the formatting string and seldom used, so it was a good candidate for culling when space got tight in the firmware. It is fairly easy to impliment whatever desired formatting with a bit of imagination. I always disliked PRINT USING for its lack of vision - the writers of the code never conceievd that you might want a formatted string to do something else with and instead make USING() a function - the syntax of which (if you wanted to print its output) would be almost identical PRINT USING$(.....) but you could also say B$=USING$(.....) - much more flexible and the code is there, they just needed to put a function call "wrapper" around it. If they had gone down this route, we could have dispensed with the weak LSET & RSET - again inexplicably implimented as statements not functions. my 2p h Edited 2024-08-04 04:25 by CaptainBoing |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6781 |
We have the FORMAT$() function in MMBasic. It's useful for numbers. It doesn't help with proportional fonts though. . Edited 2024-08-04 04:35 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6097 |
I don't think Zeitfest is printing to a MMBasic driven display. Without knowing the capabilities of his display, it is difficult to suggest a solution. The space character width can be a pain with proportional fonts but with some font families, the numbers also vary in width. Changing to a fixed width font for tables of numbers is the safest and easiest but if "printing" to HTML, consecutive spaces will get screwed up so '& nbsp ;' or similar is required to keep the alignment. Likewise, printing tables to PDF causes more grief and much head banging. I haven't found the magic required for PDFs yet. Jim VK7JH MMedit MMBasic Help |
||||
toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 339 |
Standard BASIC has just such a function: USING$(a$,x) The string representing x using a$ as a format item. Format is common with PRINT USING. a$ can not hold extra characters except a format. |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6781 |
I do wish people wouldn't refer to "standard BASIC". There was never (and still isn't) such a thing. :) The closest to a standard (and whose commands appear in all subsequent versions) was the original Dartmouth BASIC, which certainly didn't have USING. :) As far as I know PRINT USING didn't appear until Microsoft's GW-BASIC dialect (or possibly BASICA, I'm not sure), which was a disk-based one. . Edited 2024-08-04 16:59 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
Its true I am not using MMBasic (boo, hiss ) but it's always interesting to see the methods etc used, inevitably most systems follow similar patterns which can be useful The situation is, to display a number on a lcd screen, the software puts a string of characters at a position from x,y. If the string has space characters they are printed as spaces. A right-justified number is sent to the screen padded with leading spaces - but the space character is not the same width as a digit, so changing numbers alters the number of leading spaces and the visible start position does not synchronize with digit columns. So numbers don't align up/down the page which does make the bean counters tetchy . It gets worse - numbers can have -,+,e,brackets etc which can be different widths again. Maybe best to split it up. eg break the number to get the decimal point, find the preceeding text string width and compensate to set the new x,y starting point and display from that. Hmm. Edited 2024-08-04 18:28 by zeitfest |
||||
Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1611 |
Do you have to use a proportional font? Can you switch to Courier New to print the numbers? Bill Edited 2024-08-04 19:04 by Turbo46 Keep safe. Live long and prosper. |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6781 |
Not a good idea to use proportional for numbers as 1 is much narrower than 0. If you use kerfing to make the characters look right then the actual text length can be anything - it's not related to the numeric value. 111.1 is narrow, 800.8 is huge. Ideally, for aligned decimal points in a column, you'd need to print to the right from the decimal point and to the left from the same point. That takes some serious pixel-level graphics handling. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6097 |
Some languages have a function for letting you know the string length. Get the length of the string portion before the decimal and that will tell you the starting point for the PRINT. VK7JH MMedit MMBasic Help |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
I have tried a few fonts, some are proportional but deliberately have regular equal-width digits for alignment which is good. But usually the space/-/+/e character widths still vary. I could alter the relevant bits of a font to be equal width I guess. But sounds much better |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3150 |
Can you provide details--device, language used, what display, what set of fonts? A well-designed proportional font which was intended to be able to display numbers in aligned columns (one for which the designers understood the problem you are facing) would have numerals and "+", "-" (or en-dash), "." all the same width, and would also provide a space character of that width. For instance, in html there are em-space and en-space ("& emsp;" and "& ensp;" (without the spaces, which are included here to prevent them from being rendered as " " and " "). The Xerox designers of the original WYSIWYG system, Alto/Bravo, understood this and made such provisions. For instance, in addition to em-space and en-space, there would be em-dash and en-dash. In html they are represented by "& mdash;" and "& ndash;" (without the spaces, which are included here to prevent them from being rendered as "—" and "–"). In html, as indicated above, you can also build tables to get your decimal-point-aligned numbers. ~ Edited 2024-08-04 22:12 by lizby PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1611 |
You may have tried several fonts but have tried Courier New? It is the only monospaced font supplied with windows. I am convinced it will do what you want. Bill Keep safe. Live long and prosper. |
||||
toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 339 |
The closest to a standard (and whose commands appear in all subsequent versions) was the original Dartmouth BASIC, which certainly didn't have USING. :) As far as I know PRINT USING didn't appear until Microsoft's GW-BASIC dialect (or possibly BASICA, I'm not sure), which was a disk-based one. . There is a Standard BASIC. The BASIC approved by the ANSI and ISO committees is the standard for BASIC, whether anyone follows it or not. Decimal BASIC is currently the closest to the standard, although it relaxes the standard by allowing more than three subscripts and line numbers are optional. It also has switches to make it more Microsoft compatible, although if I want to run Microsoft BASIC programs, I'll use QB64. PRINT USING is in the standard as is the USING$ function. Both predate the 1983 standard for BASIC. DEC BASIC had PRINT USING before Microsoft was a company. In fact, Gates, Allen and Davidoff modeled Altair BASIC (which later became Microsoft BASIC) on DEC BASIC Plus. |
||||
Page 1 of 2 |
Print this page |