View previous topic :: View next topic |
Author |
Message |
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
Formatted output |
Posted: Wed Apr 25, 2007 6:04 am |
|
|
Hi!
I have to send to a serial printer a formatted output like this:
Code: |
Field1 Field2 Field3
2 2.6 12.5
1 12.6 876.6
|
I can't uses spaces because I don't know the length of each number and I want the dot aligned.
What do you suggest me?
Thanks
Marco / iw2nzm |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Apr 25, 2007 6:20 am |
|
|
You can use the %w option of the printf formatting capabilities.
(CCS Manual P. 186)
Humberto |
|
|
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
|
Posted: Wed Apr 25, 2007 6:32 am |
|
|
I was unclear in my previous post.
My problem is how to align the dot of numbers without to know their length.
Also, I don't need leading zeros so I can't use fixed length format.
Thanks for your quick answer!
Marco / iw2nzm |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Apr 25, 2007 7:39 am |
|
|
If you canĀ“t use spaces, nor leading zeros nor fixed length, you need to write a
dedicated function equivalent to gotoxy(x,y) but with only 1 parameter.
Humberto |
|
|
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
|
Posted: Wed Apr 25, 2007 7:51 am |
|
|
Ok, this is what I need to know.
Thanks again
Marco / iw2nzm |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 25, 2007 9:59 am |
|
|
You can use a fixed length format.
This is the difference between (on a float), '%5.1f', and '%05.1f'. The former generates a 5 digit wide field, _without_ leading spaces, while the latter adds the leading spaces.
The former format, is commonly used to generate exactly the sort of output bing described.
Best Wishes |
|
|
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
|
Posted: Wed Apr 25, 2007 11:47 am |
|
|
I used the '0' option in the printf format with integers but in that case it adds leading zeros instead spaces.
I don't know this behavior with float, thank you for the tip!
Marco / iw2nzm |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 26, 2007 3:24 am |
|
|
You do understand the three possibilities?.
%d
This will print a field as wide as needed. Changing output length 'on the fly', according to the size of the number.
%4d
This will print four characters, with leading spaces.
%04d
This will print four characters, with leading zeros.
The key to remember though, is that the field must be wide enough. Remember that a number if 'signed', can have a - sign, so a standard 8bit signed value, must be given four spaces, to handle every situation, without the field changing width. If you specify:
%3d
Then have the number -100, four characters are needed, and the output will change width. The same applies with the '%w' format, where you have to allow space, both for the sign, and the decimal point, so, to make the field stay the same width, with every possible combination on a 16bit number, using %lw, you need to make the field 7 characters wide!. The commonest fault, is underestimating just how big the field needs to be. So (for instance), on a 'float', using:
%3.1f
Only allows numbers up to '9.9' to be handled without the field growing. If instead, you want to handle numbers up to 100.0, and allow for a -ve sign as well, you need to use %6.1f...
There are faults on some compiler versions, with the width specifiers not working properly, but on the current compiler, the original poster, can use these to give fixed output widths, and align the integers, and the decimal point as required.
Best Wishes |
|
|
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
|
Posted: Thu Apr 26, 2007 3:39 am |
|
|
Ttelmah wrote: | You do understand the three possibilities?. |
I thank you very much for the time spent to write the detailed answer!
I understand, now!
Marco / iw2nzm |
|
|
|