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
On Thursday, December 31, 2020 at 11:27:26 AM UTC+2, F Bek wrote:
Thanks Sergio for the tip, it was quite illuminating. Though it left me wondering how am I going to do that. I went browsing TextEdit and TypeConverter, and also read chapter 7 of 'the Dolphin Smalltalk Companion' book to form a plan. Working on it now
and will post the code when done.
FB
On Sunday, December 27, 2020 at 11:00:46 PM UTC+2, [email protected] wrote:
El Sunday, December 27, 2020 a la(s) 8:37:44 AM UTC-3, [email protected] escribió:
Now that I have a working view, how do I format the numbers to display in a certain way? Say #,###.##
Hi FB.
TextEdit has a typeconverter property. You can create a NumberToText subclass to format the numbers as you wish and use it in your views.
-- Sergio Del Franco.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)