Search code examples
smalltalktostringvisualworks

How to convert a number to a string in Smalltalk (visual works)


I am having difficulty finding information on how to get a string representation of a number in Cincom Smalltalk. How is this performed in this language? Specifically I'm composing a string representation of an object, similar to the toString function in Java.


Solution

  • The printString method is meant to return a string to allow a programmer to interpret the number. If you used 3.14d in your example (a Double), you would see the 'd' character in the printString.

    This is fine for developers but if you're building a user interface for other people to use, you should use a PrintConverter. Here's an example:

    (PrintConverter for: #number withFormatString: '$#,###.##;[RED]($#,###.##);$0.00')
    formatStringFor: -35675.389d
    

    If you inspect the result, you'll see that it's red text in parentheses (because it's negative) and rounded to two decimal places with a leading dollar sign. This is a much better kind of number to display for a user interface. For information on the formatting characters, see the class comment for NumberPrintPolicy.