<br><br><div class="gmail_quote">On Tue, Feb 17, 2009 at 10:44 AM, Sorensen <span dir="ltr">&lt;<a href="mailto:sorens@surfy.net">sorens@surfy.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Is there a printf/sprintf-like package for formatting text &nbsp;for Squeak?<br>
<br>
If not, how do Squeakers go about formatting currency amounts or left<br>
justifying text within a field?<br>
<font color="#888888"></font></blockquote><div><br><br>My initial reaction was &quot;that&#39;s obvious; there are methods in the String class&quot;, but when I looked I realised that this isn&#39;t actually as trivial as that.<br>
<br>For printf-like functionality, class String has a &quot;formatting&quot; category with undocumented methods. The excellent &quot;Squeak by Example&quot; book has some examples (<a href="http://scg.unibe.ch/SBE/SBE.pdf">http://scg.unibe.ch/SBE/SBE.pdf</a>, page 208) but the functionality is very limited compared with printf.<br>
<br>See also SequenceableCollection&gt;&gt;copyReplaceAll:with:<br><br>For formatting currency, I notice there&#39;s a Locale class with Locale&gt;&gt;primCurrencySymbol, but I can&#39;t find any more advanced methods for actually formatting a currency, and I can&#39;t find any currency classes for any Smalltalk dialect on Google (!), apart from an LcMonetary class for GNU Smalltalk.<br>
</div></div><br>For left-justifying text, I&#39;m a bit surprised that you&#39;d want to do this. Typically, you&#39;d make the text left or right justified in whatever GUI element that value ends up in, rather than padding it with spaces.<br>
<br>Anyway; some code examples for actually doing the above:<br><br>Formatting a currency (specifically in my Locale) with two decimal places:<br><br>&quot; I assume that you&#39;re using ScaledDecimals for the currency; you generally shouldn&#39;t use Floats for currency. &quot;<br>
c := ScaledDecimal newFromNumber: 123.45 scale: 2.<br>&quot; It&#39;s good practise to use a stream for formatting strings manually &quot;<br>stream := WriteStream on: (String new: 30).<br>c &gt;= 0 ifTrue: [ stream nextPut: $- ].<br>
stream nextPutAll: Locale current primCurrencySymbol. &quot; Generally, prim methods should be private... &quot;<br>c printOn: stream. &quot; Bug: also prints out &#39;s2&#39;. &quot;<br>result := stream contents.<br clear="all">
<br>I would also add a new method to ScaledDecimal to print out its value without appending an &#39;s2&#39;. In fact, I&#39;d probably make a subclass of Number or ScaledDecimal called &quot;Currency&quot; and release it as a reusable package.<br>
<br>Left-justifying text with padding:<br><br>padding := String new: 40.<br>padding replaceAll: (Character value: 0) with: $ .<br><br>s := &#39;format me&#39;.<br>result := padding copy replaceFrom: 1 to: s size with: s.<br>
<br>Gulik.<br><br><br>-- <br><a href="http://people.squeakfoundation.org/person/mikevdg">http://people.squeakfoundation.org/person/mikevdg</a><br><a href="http://gulik.pbwiki.com/">http://gulik.pbwiki.com/</a><br>