VASmalltalk – ICU: NumberFormating

now to a pretty boring topic: number formating. When writing this code I stepped into the VA code page problems I mentioned in earlier posting.

Formating numbers (double, integers) are the most boring stuff, but with currencies it becomes more interesting.

When parsing or formating numbers with ICU you need instances of ICUNumberFormat.

Here are two code examples – please notice the different locales in the examples (and therefore the different positions of the EUR sign). The code itselfs is pretty low-level wrapper stuff:

| anUNumberFormat result res |

"please notice the codepoint 160 !"
res := '12.345,12',((Character value: 160) asString),'€'.
		
anUNumberFormat := UNumberFormat 
                     openWithStyle: ICUConstants::UNUM_CURRENCY 
                     locale: 'de_DE' 
                     pattern: nil.
result := anUNumberFormat 
            formatDoubleCurrency: 12345.12 
            currency: 'EUR'. 
anUNumberFormat close.
	
self assert: (result = res asUnicodeString)

and now the same with locale ‘en_EN’:

| anUNumberFormat result |
		
anUNumberFormat := UNumberFormat 
                      openWithStyle: ICUConstants::UNUM_CURRENCY 
                      locale: 'en_EN' 
                      pattern: nil.
result := anUNumberFormat 
            formatDoubleCurrency: 12345.12 
            currency: 'EUR'. 
anUNumberFormat close.
	
self assert: (result = '€12,345.12' asUnicodeString)
This entry was posted in Smalltalk and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s