Number formatting printf/sprintf for Squeak?

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

Number formatting printf/sprintf for Squeak?

Sorensen
Is there a printf/sprintf-like package for formatting text  for Squeak?

If not, how do Squeakers go about formatting currency amounts or left justifying text within a field?


Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Michael van der Gulik-2


On Tue, Feb 17, 2009 at 10:44 AM, Sorensen <[hidden email]> wrote:

Is there a printf/sprintf-like package for formatting text  for Squeak?

If not, how do Squeakers go about formatting currency amounts or left
justifying text within a field?


My initial reaction was "that's obvious; there are methods in the String class", but when I looked I realised that this isn't actually as trivial as that.

For printf-like functionality, class String has a "formatting" category with undocumented methods. The excellent "Squeak by Example" book has some examples (http://scg.unibe.ch/SBE/SBE.pdf, page 208) but the functionality is very limited compared with printf.

See also SequenceableCollection>>copyReplaceAll:with:

For formatting currency, I notice there's a Locale class with Locale>>primCurrencySymbol, but I can't find any more advanced methods for actually formatting a currency, and I can't find any currency classes for any Smalltalk dialect on Google (!), apart from an LcMonetary class for GNU Smalltalk.

For left-justifying text, I'm a bit surprised that you'd want to do this. Typically, you'd make the text left or right justified in whatever GUI element that value ends up in, rather than padding it with spaces.

Anyway; some code examples for actually doing the above:

Formatting a currency (specifically in my Locale) with two decimal places:

" I assume that you're using ScaledDecimals for the currency; you generally shouldn't use Floats for currency. "
c := ScaledDecimal newFromNumber: 123.45 scale: 2.
" It's good practise to use a stream for formatting strings manually "
stream := WriteStream on: (String new: 30).
c >= 0 ifTrue: [ stream nextPut: $- ].
stream nextPutAll: Locale current primCurrencySymbol. " Generally, prim methods should be private... "
c printOn: stream. " Bug: also prints out 's2'. "
result := stream contents.

I would also add a new method to ScaledDecimal to print out its value without appending an 's2'. In fact, I'd probably make a subclass of Number or ScaledDecimal called "Currency" and release it as a reusable package.

Left-justifying text with padding:

padding := String new: 40.
padding replaceAll: (Character value: 0) with: $ .

s := 'format me'.
result := padding copy replaceFrom: 1 to: s size with: s.

Gulik.


--
http://people.squeakfoundation.org/person/mikevdg
http://gulik.pbwiki.com/

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Lukas Renggli
Seaside 2.9alpha2 (builder.seaside.st) has a sophisticated framework
to build formatting objects. On of the tests for example look like
this, but there is much more that you can do:

testSwissCurrency
        | printer |
        printer := WASequentialPrinter new , 'CHF ' , WASignPrinter new ,
(WANumberPrinter new separator: $'; precision: 2; accuracy: 0.05;
yourself).
       
        self assert: (printer print: 12.34) = 'CHF 12.35'.
        self assert: (printer print: -12.39) = 'CHF -12.40'

Cheers,
Lukas

On Mon, Feb 16, 2009 at 11:44 PM, Michael van der Gulik
<[hidden email]> wrote:

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



--
Lukas Renggli
http://www.lukas-renggli.ch
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Michael van der Gulik-2
In reply to this post by Michael van der Gulik-2


On Tue, Feb 17, 2009 at 11:44 AM, Michael van der Gulik <[hidden email]> wrote:


c := ScaledDecimal newFromNumber: 123.45 scale: 2.

A better version (which I just discovered):

c := 123.45s2.

Gulik.

--
http://gulik.pbwiki.com/

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Zulq Alam-2
In reply to this post by Lukas Renggli
Hi Lukas,

Lukas Renggli wrote:
>
> testSwissCurrency
> | printer |
> printer := WASequentialPrinter new , 'CHF ' , WASignPrinter new ,
> (WANumberPrinter new separator: $'; precision: 2; accuracy: 0.05;
> yourself).
>
> self assert: (printer print: 12.34) = 'CHF 12.35'.
> self assert: (printer print: -12.39) = 'CHF -12.40'

Are there any plans to develop this into a more compact form?

For example (ugly, sorry!): 'CHF {f+-,.05}' % #(12.34).

I'm always disappointed with Squeak's string formatting/generation
facilities and if Seaside has this framework, maybe it is a good base
for compact string format language?

Thanks,
Zulq.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Sorensen
In reply to this post by Sorensen
Thanks everybody for your replies.  I'll look at SBE and Seaside 2.9 as well for further examples.

> For left-justifying text, I'm a bit surprised that you'd want to do this. Typically, you'd make the text left
> or right justified in whatever GUI element that value ends up in, rather than padding it with spaces.

You're right.  I'd like to do more than simply left-justify:

   "%6.2f"      34.56     "space-padded right justified"
   "%-6.2f"  34.56        "space-padded left justified"
   "%02d"    05            "zero padded left justified"

etc

Your examples though gave me a good place to start.

Many thanks

Sorensen wrote
Is there a printf/sprintf-like package for formatting text  for Squeak?

If not, how do Squeakers go about formatting currency amounts or left justifying text within a field?

Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

David Finlayson
There are also some interesting examples of string formatting in the
DateAndTime class used for printing hours minutes and seconds onto a
stream. That's where I picked up how Squeak does it (painful I might
add).

printf (or Java's String.format) are nice. Python has some nice ideas
here too. Maybe some Smalltalk guru could shed some light on the one
true way to do this?

David

On Mon, Feb 16, 2009 at 7:15 PM, Sorensen <[hidden email]> wrote:

>
> Thanks everybody for your replies.  I'll look at SBE and Seaside 2.9 as well
> for further examples.
>
>> For left-justifying text, I'm a bit surprised that you'd want to do this.
>> Typically, you'd make the text left
>> or right justified in whatever GUI element that value ends up in, rather
>> than padding it with spaces.
>
> You're right.  I'd like to do more than simply left-justify:
>
>   "%6.2f"      34.56     "space-padded right justified"
>   "%-6.2f"  34.56        "space-padded left justified"
>   "%02d"    05            "zero padded left justified"
>
> etc
>
> Your examples though gave me a good place to start.
>
> Many thanks
>
>
> Sorensen wrote:
>>
>> Is there a printf/sprintf-like package for formatting text  for Squeak?
>>
>> If not, how do Squeakers go about formatting currency amounts or left
>> justifying text within a field?
>>
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Number-formatting-printf-sprintf-for-Squeak--tp22045889p22049206.html
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>



--
David Finlayson, Ph.D.
Operational Geologist

U.S. Geological Survey
Pacific Science Center
400 Natural Bridges Drive
Santa Cruz, CA  95060, USA

Office: 831-427-4757
Lab: 831-427-4462
Fax: 831-427-4748
E-mail: [hidden email]
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

hernanmd
Hello David,
  You can start reading about supporting the geometric segmentation
algorithm (i.e. indentation alphabets) for structuring text documents.
Cheers.

Hernán

2009/2/17 David Finlayson <[hidden email]>:

> There are also some interesting examples of string formatting in the
> DateAndTime class used for printing hours minutes and seconds onto a
> stream. That's where I picked up how Squeak does it (painful I might
> add).
>
> printf (or Java's String.format) are nice. Python has some nice ideas
> here too. Maybe some Smalltalk guru could shed some light on the one
> true way to do this?
>
> David
>
> On Mon, Feb 16, 2009 at 7:15 PM, Sorensen <[hidden email]> wrote:
>>
>> Thanks everybody for your replies.  I'll look at SBE and Seaside 2.9 as well
>> for further examples.
>>
>>> For left-justifying text, I'm a bit surprised that you'd want to do this.
>>> Typically, you'd make the text left
>>> or right justified in whatever GUI element that value ends up in, rather
>>> than padding it with spaces.
>>
>> You're right.  I'd like to do more than simply left-justify:
>>
>>   "%6.2f"      34.56     "space-padded right justified"
>>   "%-6.2f"  34.56        "space-padded left justified"
>>   "%02d"    05            "zero padded left justified"
>>
>> etc
>>
>> Your examples though gave me a good place to start.
>>
>> Many thanks
>>
>>
>> Sorensen wrote:
>>>
>>> Is there a printf/sprintf-like package for formatting text  for Squeak?
>>>
>>> If not, how do Squeakers go about formatting currency amounts or left
>>> justifying text within a field?
>>>
>>>
>>>
>>>
>>
>> --
>> View this message in context: http://www.nabble.com/Number-formatting-printf-sprintf-for-Squeak--tp22045889p22049206.html
>> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>
>
>
>
> --
> David Finlayson, Ph.D.
> Operational Geologist
>
> U.S. Geological Survey
> Pacific Science Center
> 400 Natural Bridges Drive
> Santa Cruz, CA  95060, USA
>
> Office: 831-427-4757
> Lab: 831-427-4462
> Fax: 831-427-4748
> E-mail: [hidden email]
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
cbc
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

cbc
In reply to this post by Michael van der Gulik-2
I'd be leary of basing Currency off of ScaledDecimal, especially if there is to be much manipulation of it (such as multiplication or division).  The reason is that ScaledDecimal keeps the representation of their numbers as fractions under the covers - which can results in some odd (and incorrect) results.
 
There is another package (not part of standard Squeak) that could be more suitable as a basis for Currency: FixedDecimal, located at http://www.squeaksource.com/FixedDecimal .  The code isn't very pretty (sorry about that), but it does work for currency.  That is the reason it was originally written - when I started noticing the results of simple mamipulation of my money amounts where wrong.
 
-Chris

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Tapple Gao
On Wed, Feb 18, 2009 at 11:28:29AM -0800, Chris Cunningham wrote:
> I'd be leary of basing Currency off of ScaledDecimal, especially if there is
> to be much manipulation of it (such as multiplication or division).  The
> reason is that ScaledDecimal keeps the representation of their numbers as
> fractions under the covers - which can results in some odd (and incorrect)
> results.

ScaledDecimal is also probably going to be removed from squeak
soon, as it has licensing issues.

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

David T. Lewis
On Wed, Feb 18, 2009 at 02:37:27PM -0500, Matthew Fulmer wrote:
> On Wed, Feb 18, 2009 at 11:28:29AM -0800, Chris Cunningham wrote:
> > I'd be leary of basing Currency off of ScaledDecimal, especially if there is
> > to be much manipulation of it (such as multiplication or division).  The
> > reason is that ScaledDecimal keeps the representation of their numbers as
> > fractions under the covers - which can results in some odd (and incorrect)
> > results.
>
> ScaledDecimal is also probably going to be removed from squeak
> soon, as it has licensing issues.

Arrgh! I started using ScaledDecimal for representing time magnitudes
in TimeZoneDatabase <http://wiki.squeak.org/squeak/1076> quite some
time ago, and it works very well and is quite efficient. Are the
licensing issues resolvable? Can I help?

Dave

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Yoshiki Ohshima-2
At Wed, 18 Feb 2009 22:34:08 -0500,
David T. Lewis wrote:

>
> On Wed, Feb 18, 2009 at 02:37:27PM -0500, Matthew Fulmer wrote:
> > On Wed, Feb 18, 2009 at 11:28:29AM -0800, Chris Cunningham wrote:
> > > I'd be leary of basing Currency off of ScaledDecimal, especially if there is
> > > to be much manipulation of it (such as multiplication or division).  The
> > > reason is that ScaledDecimal keeps the representation of their numbers as
> > > fractions under the covers - which can results in some odd (and incorrect)
> > > results.
> >
> > ScaledDecimal is also probably going to be removed from squeak
> > soon, as it has licensing issues.
>
> Arrgh! I started using ScaledDecimal for representing time magnitudes
> in TimeZoneDatabase <http://wiki.squeak.org/squeak/1076> quite some
> time ago, and it works very well and is quite efficient. Are the
> licensing issues resolvable? Can I help?

  1.  Try to find  Richard A. Harmon.  I for example looked up the
      name in Skype and send text chat to all of them, but didn't get
      any response.  If you know other ways, please try.

  2.  Write a new package.

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Yoshiki Ohshima-2
At Thu, 19 Feb 2009 12:11:33 -0800,
Yoshiki Ohshima wrote:

>
> At Wed, 18 Feb 2009 22:34:08 -0500,
> David T. Lewis wrote:
> >
> > On Wed, Feb 18, 2009 at 02:37:27PM -0500, Matthew Fulmer wrote:
> > > On Wed, Feb 18, 2009 at 11:28:29AM -0800, Chris Cunningham wrote:
> > > > I'd be leary of basing Currency off of ScaledDecimal, especially if there is
> > > > to be much manipulation of it (such as multiplication or division).  The
> > > > reason is that ScaledDecimal keeps the representation of their numbers as
> > > > fractions under the covers - which can results in some odd (and incorrect)
> > > > results.
> > >
> > > ScaledDecimal is also probably going to be removed from squeak
> > > soon, as it has licensing issues.
> >
> > Arrgh! I started using ScaledDecimal for representing time magnitudes
> > in TimeZoneDatabase <http://wiki.squeak.org/squeak/1076> quite some
> > time ago, and it works very well and is quite efficient. Are the
> > licensing issues resolvable? Can I help?
>
>   1.  Try to find  Richard A. Harmon.  I for example looked up the
>       name in Skype and send text chat to all of them, but didn't get
>       any response.  If you know other ways, please try.
>
>   2.  Write a new package.

  And also:

    3.  Port somebody else code.

    4.  Apparently, Mark Schwenk once released his code.
        (http://lists.squeakfoundation.org/pipermail/squeak-dev/1999-June/021113.html)
        If we can get his code, we see how well it works and possibly
        put it in instead.

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

David T. Lewis
In reply to this post by Yoshiki Ohshima-2
On Thu, Feb 19, 2009 at 12:11:33PM -0800, Yoshiki Ohshima wrote:

> At Wed, 18 Feb 2009 22:34:08 -0500,
> David T. Lewis wrote:
> >
> > Arrgh! I started using ScaledDecimal for representing time magnitudes
> > in TimeZoneDatabase <http://wiki.squeak.org/squeak/1076> quite some
> > time ago, and it works very well and is quite efficient. Are the
> > licensing issues resolvable? Can I help?
>
>   1.  Try to find  Richard A. Harmon.  I for example looked up the
>       name in Skype and send text chat to all of them, but didn't get
>       any response.  If you know other ways, please try.
>
>   2.  Write a new package.

Hi Yoshiki,

I'll see if I can contact Richard A Harmon, and I'll let you know if
I have any success.

Thanks,
Dave

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Steven Greenberg
There's a Printf module on squeaksource that works fine. 

http://www.squeaksource.com/Printf.html




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Number formatting printf/sprintf for Squeak?

Sorensen
What a great find.  Thank you.

Steven Greenberg wrote
There's a Printf module on squeaksource that works fine.

http://www.squeaksource.com/Printf.html

_______________________________________________
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners