0

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

Aditya Kappagantula
  • 564
  • 1
  • 7
  • 21
  • As far as I can tell `printFormat:` is implemented only in _Date_. Do you have something special on your mind or you just want a method to print number in a special way? – Uko Nov 11 '13 at 10:21
  • I wanted to print it in currency format. I just found that it can be done like this: aNumber printFormat: $#,###.00 However there exists a floating point round off error with above implementation for 123456.123123 – Aditya Kappagantula Nov 11 '13 at 10:28
  • So you can either submit a bug report or fix it :) – Uko Nov 11 '13 at 11:40
  • 1
    possible duplicate of [How to convert a number to a string in Smalltalk (visual works)](http://stackoverflow.com/questions/19104125/how-to-convert-a-number-to-a-string-in-smalltalk-visual-works) – David Buck Nov 11 '13 at 11:47

2 Answers2

0

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

Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65
0

[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.

vmusulainen
  • 328
  • 3
  • 7