Printf class

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Printf class

Joseph Alotta
Greetings,

I downloaded the Printf package.  I found it on Squeaksource and installed it with a file in.  It is not on SqueakMap or Monticello.

It is running, but doesn't seem to work.   Missing a lot of documentation.

Does anyone know how to use this?

'12.343434' printf: '%3.2s' ==> '12.343434'


Is there another print formatting class I should be using?

Sincerely,

Joe.



Reply | Threaded
Open this post in threaded view
|

Re: Printf class

Balázs Kósi-2
Hi,

I took a cursory look at the Printf package. You should send #printf:
to the format string, with the data to be formatted as the arguments.
There are some examples on the class side of FormatString. Your
example could be written as:
'%6.2e' printf: 12.343434 resulting in: ' 12.34'.

String understands #format: which is the basic string interpolation
method in Squeak, but it doesn't do any number formatting. The same
formatting as above could be achieved with methods already in the
trunk like this:
(12.343434 printShowingMaxDecimalPlaces: 2) padded: #left to: 6 with:
Character space

Maybe there is a more concise way?

HTH, Balázs

Reply | Threaded
Open this post in threaded view
|

Re: Printf class

Joseph Alotta
In reply to this post by Joseph Alotta
Hi Balázs,

Thank you very much for your answer, it helped a lot.  Sending the #printf message to the string seems a little counterintuitive.

How does the interpretor know that I mean to create an instance of the class FormatString, instead of String?

I looked at the Class definition and it just says that Format String is a subclass of Object.  So how does the code
get executed?

If I can figure out how to put a method into the String class, then I can use the long worded code you have below and just
name it something easier.

Thanks so much,

Sincerely,

Joe.




> ------------------------------
>
> Message: 2
> Date: Sat, 6 Oct 2012 20:29:50 +0200
> From: Bal?zs K?si <[hidden email]>
> Subject: Re: [squeak-dev] Printf class
> To: The general-purpose Squeak developers list
> <[hidden email]>
> Message-ID:
> <CACeQCFaO4d8vvmLR4BAnYZjENeGaZd1q-aNK=[hidden email]>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hi,
>
> I took a cursory look at the Printf package. You should send #printf:
> to the format string, with the data to be formatted as the arguments.
> There are some examples on the class side of FormatString. Your
> example could be written as:
> '%6.2e' printf: 12.343434 resulting in: ' 12.34'.
>
> String understands #format: which is the basic string interpolation
> method in Squeak, but it doesn't do any number formatting. The same
> formatting as above could be achieved with methods already in the
> trunk like this:
> (12.343434 printShowingMaxDecimalPlaces: 2) padded: #left to: 6 with:
> Character space
>
> Maybe there is a more concise way?
>
> HTH, Balázs

Reply | Threaded
Open this post in threaded view
|

Re: Printf class

Balázs Kósi-2
Hi!

You can put methods into any class. The Printf package just does that:
it defines the #printf: method in the String class.
When creating your own package, the convention is to put methods
extending classes outside your package into method categories prefixed
with *YourPackageName. Monticello uses this to pick up extension
methods.

Cheers, Balázs



System Browser: String.png (39K) Download Attachment
Snapshot Browser: Printf.png (22K) Download Attachment