Ian's Dolphin Smalltalk Pages

CurrencyToText


Overview

This package provides a simple converter named CurrencyToText, a subclass in Dolphin's TypeConverter hierarchy, which provides input and output of currency values. It should be possible to use the converter in the same way as any of the exiting converters.

As with all Dolphin TypeConverters the CurrencyToText converter provides conversion in two directions via separate instance methods.

Currency to Text conversion allows the addition of formatting characters to the answered String. The following shows workspace examples for all the formatting options. If the defaults are not to your liking then they can be changed by editing the various #defaultXxx methods in the CurrencyToText class.

There are obviously lots of formatting options that would need to be added to provide a comprehensive package that would work for all currencies. I'm afraid that is outside the scope of this package but, hopefully, it might provide a basis for others to provide the formatting they need.

converter := CurrencyToText new.
amount := 123456.789.

"Default values"
converter convertFromLeftToRight: amount --> '£123,456.79'
converter convertFromLeftToRight: 0 - amount--> '£-123,456.79'

"Insert separators"
converter useSeparators: false.
converter convertFromLeftToRight: amount --> '£123456.79'
converter useSeparators: true.
converter convertFromLeftToRight: amount --> '£123,456.79'

"Change separator character"
converter integerSeparator: '@'.
converter convertFromLeftToRight: amount --> '£123@456.79'
converter integerSeparator: ','.
converter convertFromLeftToRight: amount --> '£123,456.79'

"Change grouping size"
converter integerSeparation: 4.
converter convertFromLeftToRight: amount --> '£12,3456.79'
converter integerSeparation: 3.
converter convertFromLeftToRight: amount --> '£123,456.79'

"Change currency symbol"
converter currencySymbol: $$.
converter convertFromLeftToRight: amount --> '$123,456.79'
converter currencySymbol: $£.
converter convertFromLeftToRight: amount --> '£123,456.79'

"Set the length of the fractional part"
converter fractionLength: 4.
converter convertFromLeftToRight: amount --> '£123,456.7890'
converter fractionLength: 2.
converter convertFromLeftToRight: amount --> '£123,456.79'

"Set the length of the integer part
Left fill with spaces or zero"
converter integerLength: 8.
converter convertFromLeftToRight: amount --> '£  123,456.79'
converter zeroFillInteger: true.
converter convertFromLeftToRight: amount --> '£00,123,456.79'
converter integerLength: 1.
converter zeroFillInteger: false.
converter convertFromLeftToRight: amount --> '£123,456.79'

"Insert the sign before or after the currency symbol"
converter signBeforeCurrency: true.
converter convertFromLeftToRight: 0 - amount --> '-£123,456.79'
converter signBeforeCurrency: false.
converter convertFromLeftToRight: 0 - amount --> '£-123,456.79'
			

Text to Currency conversion is much simpler. It just removes any formatting character that might have been added by the #convertFromLeftToRight operation, spaces/commas/currency symbol, and tries to convert what's left to a Number.

converter := CurrencyToText new..
converter convertFromRightToLeft: '�123,456.79' --> 123456.79
converter convertFromRightToLeft: '�-123,456.79' --> -123456.79
			

If an error occurs during a conversion then a normal Dolphin error is raised. If there is the possibility of incorrect parameters being given to the converter then these errors should be trapped using the Dolphin Error mechanism.


email address
Valid XHTML 1.1 Valid CSS!