Really wondering why Dolphin 7.xx still doesn't have native support for number formatting!!
Anyway, this is the method I wrote to handle number formatting. It certainly does not do everything but still manages to offer a few options to group digits by a chosen number, using a chosen separator, and to round the number after a chosen number of decimals and use a chosen decimal separator - I've renamed this method a couple of times, but I'm still not really happy with the current name:
Number>>asTextWithFormatBy: groupNum groupWith: groupChar decimalWith: decimalChar decimalNum: decimals
"Format a number as ###,###.## | groupNum: anInteger; groupChar: aChar; decimalChar: aChar; decimals: anInteger"
| in out count numberToString |
numberToString := (self * (10 ** decimals) roundTo: 1) printString reverse.
[numberToString size > decimals] whileFalse: [numberToString := numberToString , '0' ]. "pad with zeros"
in := ReadStream on: numberToString.
out := WriteStream on: String new.
count := 1.
[count > decimals] whileFalse:
[out nextPut: in next.
count := count + 1].
out nextPut: decimalChar. "add the decimal part and decimal separator"
count := 1.
[in atEnd] whileFalse:
[out nextPut: in next.
(count = 0 and: [in atEnd not]) ifTrue: [out nextPut: groupChar].
count := (count + 1) \\ groupNum]. "add the integer part and the digits separator"
^out contents reverse asString.
The next step was to create a new converter class, (at the same level as the system's NumberToText, I did not see an immediate advantage of subclassing it, and leaving room to avoid potential clashes with future versions of Dolphin):
AbstractToTextConverter subclass: #FB_NumberToText
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
classInstanceVariableNames: ''
FB_NumberToText class >> category: MVP-Type Converters-Text - this actually registers this class as an additional typeconverter option in TextEdit, making it available for selection.
Next, I've added this method; the choices are hard-coded for now; maybe they can be stored in Class variables and modified through the view?:
FB_NumberToText >> leftToRight: aNumber
"Answers the result of converting aNumber to a String"
^aNumber asTextWithFormatBy: 3 groupWith: $, decimalWith: $. decimalNum: 3 .
I've also added a rightToLeft method, as a copy-paste of the original method in NumberToText (yes, I could have inherited it if I subclassed).
With that, I had a working converter, that fits the current purpose. I'm sure there are ways to optimize and expand it still.
Thanks for reading and eventually commenting.
FB