1. Printf puzzlement

include std/io.e 
include std/filesys.e 
include std/error.e 
include std/console.e 
include std/graphics.e 
include std/sequence.e 
include std/cmdline.e 
integer key_code 
sequence test_sequence = "123456789" 
position (16,50) 
printf(1, "%7s", {test_sequence}) 
key_code = wait_key() 


Results in:
123456789

Why isn't it:
1234567

new topic     » topic index » view message » categorize

2. Re: Printf puzzlement

From the docs:

Field widths can be added to the basic formats, e.g. %5d, %8.2f, %10.4s. The number before the decimal point is the minimum field width to be used. The number after the decimal point is the precision to be used for numeric values.

There's no format specifier to clip strings to a given length, so you would need to do that using brackets:

printf(1,"%7s",{test_sequence[1..7]} 

Of course, the above would fail if the test_sequence was shorter than 7, so perhaps:

printf(1,"%7s",{test_sequence[1..math:min(7,length(test_sequence))]} 

If test_sequence is shorter than 7, then spaces will be added to the start of the string to make it 7 characters long.

printf(1,"%7s",{test_sequence}) 

Also, look at text:format() and display(), they can often do more than printf() with less coding.

display("[:4]",{test_sequence}) 

The above will clip the string to the first 4 characters, and will just print it if it has less than 4 characters. See what i mean about doing more with less?

display("[:4>]",{test_sequence}) 

Above, we right-justify the printout, so blank space(s) are added to the start of the string to make it take up 4 character spaces. There are lots of other possibilities, as well. display() comes in very handy, especially when formatting reports. It's easy to position things exactly where you want them.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Printf puzzlement

Hi, another way

printf(1, "%7.7s", {string})

will print the string into an allocation of 7 spaces, clipped to a maximum of 7 characters, so

1234567

printf(1, "%7.3s", {string})

123

and printf(1, "%-7.3s", {string})

123

the '-' sign says to justify to the left, and the number after the decimal point says how many characters to print within the allocated space. The allocated space can be overflowed, so just watch for that.

Cheers

Chris

Sorry, just noted that that justification didn't show properly

printf(1, "%7.7s", {string}) 
 
will print the string into an allocation of 7 spaces, clipped to a maximum of 7 characters, so 
 
1234567 
 
printf(1, "%7.3s", {string}) 
 
    123 
 
and 
printf(1, "%-7.3s", {string}) 
 
123 

new topic     » goto parent     » topic index » view message » categorize

4. Re: Printf puzzlement

Thanks Irv and Chris. I had assumed that the decimal point applied only to precision of numerical values. An eye-opener. Allen

new topic     » goto parent     » topic index » view message » categorize

5. Re: Printf puzzlement

Updated

I feel that the art of printf is being lost in this new fangled world of gooey windows.

Chris

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu