Question

Can someone please give me an example of how to use the printFormat: method in Smalltalk to format a string?

Eg:

'123456789' printFormat: aFormat should return something like $123,456,789.00

Was it helpful?

Solution

The normal way to find examples of the usage of a method in smalltalk is to select it and then search for senders. Most smalltalks (VW, Squeak, Pharo, Dolphin, Amber etc) have a keyboard shortcut for that.

If the number is too large, you might want to take a look at implementors.

Smalltalk often has little help text and comments, but a lot of real code using a specific construct.

We try to avoid using floats in currency calculations. ScaledDecimals work better, and you might want to create a real Money class

OTHER TIPS

[VIsualWorks] Pls, see at Class NumberPrintPolicy. This class has nice comments. In your case solution may be following:

(NumberPrintPolicy defaultInstance)
    thousandsSeparator: $,;
    decimalPoint: $..
NumberPrintPolicy print: 123456789 using: '$#,###.00'

I changed thousandsSeparator and decimalPoint because I have default russian locale at VisualWorks with another values of thousandsSeparator and decimalPoint.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top