|
Formatting with a leading + / Lua and Perl
In formatted printing, you can often use a leading "+" in the format string to force a positive sign to be added in front of positive numbers - for example "%+4d" means an integer, to base 10, 4 character positions as a minimum, right justified.
Here's an example of that, in code, in Lua
for john = -10, 10, 1 do
print (string.format("xx %+4d yy",john))
end
And here is how it runs:
[trainee@easterton o8]$ lua sf
xx -10 yy
xx -9 yy
xx -8 yy
xx -7 yy
xx -6 yy
xx -5 yy
xx -4 yy
xx -3 yy
xx -2 yy
xx -1 yy
xx +0 yy
xx +1 yy
xx +2 yy
xx +3 yy
xx +4 yy
xx +5 yy
xx +6 yy
xx +7 yy
xx +8 yy
xx +9 yy
xx +10 yy
[trainee@easterton o8]$
The same technique applies in Perl, and in other languages too.
Other leading characters:
0 - Leading zero fill
- - Left justify (written 2008-10-15 11:57:46)
Associated topics are indexed under P207 - Perl - File HandlingU110 - Lua - Input / Output
Some other Articles
Passing a table from Lua into CCalling functions in C from your Lua script - a first HowToHow many cups of coffee?Lua Course, and the Wiltshire Countryside tooFormatting with a leading + / Lua and PerlValidating Credit Card NumbersJob applicationOxford in PicturesAlfred the GreatNext in the sequence - courses next year (2009)
|
1892 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|