Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  nodelist  faq  login

PURGE COMPLETE.


programming / comp.lang.smalltalk.dolphin / Re: Formatting Numbers in Views

SubjectAuthor
* Formatting Numbers in ViewsF Bek
`* Re: Formatting Numbers in ViewsSergio Del Franco
 `* Re: Formatting Numbers in ViewsF Bek
  `* Re: Formatting Numbers in ViewsF Bek
   `* Re: Formatting Numbers in ViewsSergio Del Franco
    `* Re: Formatting Numbers in ViewsF Bek
     `* Re: Formatting Numbers in ViewsF Bek
      `- Re: Formatting Numbers in ViewsF Bek

1
Subject: Formatting Numbers in Views
From: F Bek
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sun, 27 Dec 2020 11:37 UTC
X-Received: by 2002:a05:622a:243:: with SMTP id c3mr39974571qtx.202.1609069063413; Sun, 27 Dec 2020 03:37:43 -0800 (PST)
X-Received: by 2002:a05:6830:10:: with SMTP id c16mr29416823otp.162.1609069063200; Sun, 27 Dec 2020 03:37:43 -0800 (PST)
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sun, 27 Dec 2020 03:37:42 -0800 (PST)
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=77.246.71.215; posting-account=5ZCy9QoAAAAoNnwHIxbmglmR4vxJHcnW
NNTP-Posting-Host: 77.246.71.215
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com>
Subject: Formatting Numbers in Views
From: fbekd...@gmail.com (F Bek)
Injection-Date: Sun, 27 Dec 2020 11:37:43 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 9
View all headers
Beginner's question.
DS 7.1.18

Now that I have a working view, how do I format the numbers to display in a certain way?  Say #,###.##

Browsing the system, I could find an external class NUMBERFMTW that seems related, but cannot figure out how to use it, or if there is any other way.

Thanks for reading and eventually answering.

FB


Subject: Re: Formatting Numbers in Views
From: Sergio Del Franco
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sun, 27 Dec 2020 21:00 UTC
References: 1
X-Received: by 2002:a05:6214:c3:: with SMTP id f3mr44869501qvs.36.1609102845568;
Sun, 27 Dec 2020 13:00:45 -0800 (PST)
X-Received: by 2002:aca:f48b:: with SMTP id s133mr10295198oih.59.1609102845329;
Sun, 27 Dec 2020 13:00:45 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sun, 27 Dec 2020 13:00:44 -0800 (PST)
In-Reply-To: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=190.193.164.63; posting-account=yLVg6AoAAACEisDROzJKbF3TKjCK_524
NNTP-Posting-Host: 190.193.164.63
References: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com>
Subject: Re: Formatting Numbers in Views
From: sdelfra...@gmail.com (Sergio Del Franco)
Injection-Date: Sun, 27 Dec 2020 21:00:45 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
View all headers
El Sunday, December 27, 2020 a la(s) 8:37:44 AM UTC-3, fbek...@gmail.com 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.


Subject: Re: Formatting Numbers in Views
From: F Bek
Newsgroups: comp.lang.smalltalk.dolphin
Date: Thu, 31 Dec 2020 09:27 UTC
References: 1 2
X-Received: by 2002:a37:6189:: with SMTP id v131mr55147710qkb.337.1609406846124; Thu, 31 Dec 2020 01:27:26 -0800 (PST)
X-Received: by 2002:aca:48c4:: with SMTP id v187mr7371886oia.37.1609406845930; Thu, 31 Dec 2020 01:27:25 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Thu, 31 Dec 2020 01:27:25 -0800 (PST)
In-Reply-To: <becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=77.246.71.215; posting-account=5ZCy9QoAAAAoNnwHIxbmglmR4vxJHcnW
NNTP-Posting-Host: 77.246.71.215
References: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com> <becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <438a9d33-69f3-409a-a9bc-5072448cd1f1n@googlegroups.com>
Subject: Re: Formatting Numbers in Views
From: fbekd...@gmail.com (F Bek)
Injection-Date: Thu, 31 Dec 2020 09:27:26 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 17
View all headers
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, sdelf...@gmail.com wrote:
El Sunday, December 27, 2020 a la(s) 8:37:44 AM UTC-3, fbek...@gmail.com 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.


Subject: Re: Formatting Numbers in Views
From: F Bek
Newsgroups: comp.lang.smalltalk.dolphin
Date: Tue, 5 Jan 2021 14:48 UTC
References: 1 2 3
X-Received: by 2002:a37:5a47:: with SMTP id o68mr65995363qkb.423.1609858098014;
Tue, 05 Jan 2021 06:48:18 -0800 (PST)
X-Received: by 2002:aca:3194:: with SMTP id x142mr2887362oix.82.1609858097775;
Tue, 05 Jan 2021 06:48:17 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Tue, 5 Jan 2021 06:48:17 -0800 (PST)
In-Reply-To: <438a9d33-69f3-409a-a9bc-5072448cd1f1n@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=80.79.152.154; posting-account=5ZCy9QoAAAAoNnwHIxbmglmR4vxJHcnW
NNTP-Posting-Host: 80.79.152.154
References: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com>
<becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com> <438a9d33-69f3-409a-a9bc-5072448cd1f1n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <609ef980-798c-4f23-97bb-a6825e642febn@googlegroups.com>
Subject: Re: Formatting Numbers in Views
From: fbekd...@gmail.com (F Bek)
Injection-Date: Tue, 05 Jan 2021 14:48:18 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 5000
View all headers
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, sdelf...@gmail.com wrote:
El Sunday, December 27, 2020 a la(s) 8:37:44 AM UTC-3, fbek...@gmail.com 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.


Subject: Re: Formatting Numbers in Views
From: Sergio Del Franco
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sat, 9 Jan 2021 12:00 UTC
References: 1 2 3 4
X-Received: by 2002:ad4:484c:: with SMTP id t12mr7929424qvy.58.1610193655175; Sat, 09 Jan 2021 04:00:55 -0800 (PST)
X-Received: by 2002:aca:4e0a:: with SMTP id c10mr5061287oib.14.1610193654847; Sat, 09 Jan 2021 04:00:54 -0800 (PST)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!tr2.eu1.usenetexpress.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sat, 9 Jan 2021 04:00:54 -0800 (PST)
In-Reply-To: <609ef980-798c-4f23-97bb-a6825e642febn@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=190.193.164.63; posting-account=yLVg6AoAAACEisDROzJKbF3TKjCK_524
NNTP-Posting-Host: 190.193.164.63
References: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com> <becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com> <438a9d33-69f3-409a-a9bc-5072448cd1f1n@googlegroups.com> <609ef980-798c-4f23-97bb-a6825e642febn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <19649fd0-d077-4f03-a8fd-61a0b9607fc4n@googlegroups.com>
Subject: Re: Formatting Numbers in Views
From: sdelfra...@gmail.com (Sergio Del Franco)
Injection-Date: Sat, 09 Jan 2021 12:00:55 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 28
View all headers
El Tuesday, January 5, 2021 a la(s) 11:48:18 AM UTC-3, fbek...@gmail.com escribió:
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.
[...]
Number>>asTextWithFormatBy: groupNum groupWith: groupChar decimalWith: decimalChar decimalNum: decimals
"Format a number as ###,###.## | groupNum: anInteger; groupChar: aChar; decimalChar: aChar; decimals: anInteger"
[...]

You should use Locale >> #printFloat:on:format::

String streamContents: [:stream |
Locale default
printFloat: 1234.56
on: stream
format: (
NUMBERFMTW new
Grouping: 3;
thousandSeparator: ',';
decimalSeparator: '.';
decimalPlaces: 3;
yourself
).
].

-- Sergio Del Franco


Subject: Re: Formatting Numbers in Views
From: F Bek
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sat, 9 Jan 2021 15:08 UTC
References: 1 2 3 4 5
X-Received: by 2002:ac8:6658:: with SMTP id j24mr2289904qtp.374.1610204915993;
Sat, 09 Jan 2021 07:08:35 -0800 (PST)
X-Received: by 2002:aca:48c4:: with SMTP id v187mr5285255oia.37.1610204915740;
Sat, 09 Jan 2021 07:08:35 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Sat, 9 Jan 2021 07:08:35 -0800 (PST)
In-Reply-To: <19649fd0-d077-4f03-a8fd-61a0b9607fc4n@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=80.79.152.154; posting-account=5ZCy9QoAAAAoNnwHIxbmglmR4vxJHcnW
NNTP-Posting-Host: 80.79.152.154
References: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com>
<becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com> <438a9d33-69f3-409a-a9bc-5072448cd1f1n@googlegroups.com>
<609ef980-798c-4f23-97bb-a6825e642febn@googlegroups.com> <19649fd0-d077-4f03-a8fd-61a0b9607fc4n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5a841d5a-a2b7-4ae1-9e26-26dc547766cbn@googlegroups.com>
Subject: Re: Formatting Numbers in Views
From: fbekd...@gmail.com (F Bek)
Injection-Date: Sat, 09 Jan 2021 15:08:35 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
View all headers
Yes! This is what I was looking for!
Experimenting with the code now...

Thanks Sergio.

FB

On Saturday, January 9, 2021 at 2:00:55 PM UTC+2, sdelf...@gmail.com wrote:
El Tuesday, January 5, 2021 a la(s) 11:48:18 AM UTC-3, fbek...@gmail.com escribió:
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.
[...]
Number>>asTextWithFormatBy: groupNum groupWith: groupChar decimalWith: decimalChar decimalNum: decimals
"Format a number as ###,###.## | groupNum: anInteger; groupChar: aChar; decimalChar: aChar; decimals: anInteger"
[...]

You should use Locale >> #printFloat:on:format::

String streamContents: [:stream |
Locale default
printFloat: 1234.56
on: stream
format: (
NUMBERFMTW new
Grouping: 3;
thousandSeparator: ',';
decimalSeparator: '.';
decimalPlaces: 3;
yourself
).
].

-- Sergio Del Franco


Subject: Re: Formatting Numbers in Views
From: F Bek
Newsgroups: comp.lang.smalltalk.dolphin
Date: Tue, 12 Jan 2021 23:22 UTC
References: 1 2 3 4 5 6
X-Received: by 2002:a37:883:: with SMTP id 125mr2082528qki.366.1610493731789;
Tue, 12 Jan 2021 15:22:11 -0800 (PST)
X-Received: by 2002:a4a:d628:: with SMTP id n8mr892158oon.79.1610493731487;
Tue, 12 Jan 2021 15:22:11 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Tue, 12 Jan 2021 15:22:11 -0800 (PST)
In-Reply-To: <5a841d5a-a2b7-4ae1-9e26-26dc547766cbn@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=77.246.71.169; posting-account=5ZCy9QoAAAAoNnwHIxbmglmR4vxJHcnW
NNTP-Posting-Host: 77.246.71.169
References: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com>
<becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com> <438a9d33-69f3-409a-a9bc-5072448cd1f1n@googlegroups.com>
<609ef980-798c-4f23-97bb-a6825e642febn@googlegroups.com> <19649fd0-d077-4f03-a8fd-61a0b9607fc4n@googlegroups.com>
<5a841d5a-a2b7-4ae1-9e26-26dc547766cbn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6c8dc575-1f10-4eda-8880-c0e1d0f6aac6n@googlegroups.com>
Subject: Re: Formatting Numbers in Views
From: fbekd...@gmail.com (F Bek)
Injection-Date: Tue, 12 Jan 2021 23:22:11 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 84
View all headers
Implementing the new code, my number formatting method now reads like this:

Number >> asTextWithGrouping: groupNum groupSeparator: groupChar decimalSeparator: decimalChar decimalPlaces: decimals
"Format the number as ###,###.## | groupNum: anInteger; groupChar: aString; decimalChar: aString; decimals: anInteger"
^String streamContents: [:strm | Locale default printFloat: self asFloat on: strm format: (
NUMBERFMTW new Grouping: groupNum ; thousandSeparator: groupChar ;
decimalSeparator: decimalChar ; decimalPlaces: decimals ;
LeadingZero: 1 ; NegativeOrder: 1 ;
yourself).  ].

The receiver of the method is still <Number>, although it really could be any other object, and I have considered moving it to <String> instead, where it logically belongs - but that would add an argument for passing the number being formatted, so I finally decided against the change. In the above, I have added the last 2 properties (LeadingZero & NegativeOrder) more as a mental note, and for future reference, if necessary.
I then modified the converter #leftToRight method to call the new method:

 FB_NumberToText >> leftToRight: aNumber
"Answers the result of converting aNumber to a String"
^aNumber asTextWithGrouping: 3 groupSeparator: ',' decimalSeparator: '.' decimalPlaces: 2  .

The rest of he code remained the same. With those 2 changes, my formatter was functional again.

Now for a quick discussion - from a beginner's point of view:
Using Locale >> #printFloat:on:format: was interesting and revealing but did not really do much more than my humble converter, included in the previous message, was able to offer (except for the possibility to auto format numbers according to the Windows local default). It remains very short of a full number formatting solution, similar to Windows products, e.g. in Excel. Again, for a programming environment that targets Windows exclusively, I thought Dolphin would offer a way to tap into all of this readily. Now it seems I have to further expand the method if I need some additional functionality - unless I am missing something again. Nevertheless, I am happy with the result so far as it fits the current need.

Thanks Sergio for the assistance, happy hear your feedback.

FB

 

On Saturday, January 9, 2021 at 5:08:36 PM UTC+2, F Bek wrote:
Yes! This is what I was looking for!
Experimenting with the code now...

Thanks Sergio.

FB
On Saturday, January 9, 2021 at 2:00:55 PM UTC+2, sdelf...@gmail.com wrote:
El Tuesday, January 5, 2021 a la(s) 11:48:18 AM UTC-3, fbek...@gmail.com escribió:
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.
[...]
Number>>asTextWithFormatBy: groupNum groupWith: groupChar decimalWith: decimalChar decimalNum: decimals
"Format a number as ###,###.## | groupNum: anInteger; groupChar: aChar; decimalChar: aChar; decimals: anInteger"
[...]

You should use Locale >> #printFloat:on:format::

String streamContents: [:stream |
Locale default
printFloat: 1234.56
on: stream
format: (
NUMBERFMTW new
Grouping: 3;
thousandSeparator: ',';
decimalSeparator: '.';
decimalPlaces: 3;
yourself
).
].

-- Sergio Del Franco


Subject: Re: Formatting Numbers in Views
From: F Bek
Newsgroups: comp.lang.smalltalk.dolphin
Date: Fri, 12 Feb 2021 19:16 UTC
References: 1 2 3 4 5 6 7
X-Received: by 2002:a05:6214:452:: with SMTP id cc18mr3897934qvb.26.1613157414371;
Fri, 12 Feb 2021 11:16:54 -0800 (PST)
X-Received: by 2002:a05:6808:294:: with SMTP id z20mr687963oic.14.1613157413918;
Fri, 12 Feb 2021 11:16:53 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.smalltalk.dolphin
Date: Fri, 12 Feb 2021 11:16:53 -0800 (PST)
In-Reply-To: <6c8dc575-1f10-4eda-8880-c0e1d0f6aac6n@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: google-groups.googlegroups.com; posting-host=80.79.145.136; posting-account=5ZCy9QoAAAAoNnwHIxbmglmR4vxJHcnW
NNTP-Posting-Host: 80.79.145.136
References: <fbbe6ec3-d362-4e29-b389-59b0b3c835c1n@googlegroups.com>
<becd6f5f-c5ff-47ff-9738-27d20df27d2dn@googlegroups.com> <438a9d33-69f3-409a-a9bc-5072448cd1f1n@googlegroups.com>
<609ef980-798c-4f23-97bb-a6825e642febn@googlegroups.com> <19649fd0-d077-4f03-a8fd-61a0b9607fc4n@googlegroups.com>
<5a841d5a-a2b7-4ae1-9e26-26dc547766cbn@googlegroups.com> <6c8dc575-1f10-4eda-8880-c0e1d0f6aac6n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1c367567-c983-4051-838a-4f75a4c8f87en@googlegroups.com>
Subject: Re: Formatting Numbers in Views
From: fbekd...@gmail.com (F Bek)
Injection-Date: Fri, 12 Feb 2021 19:16:54 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
View all headers
Note: with the new Dolphin version 7.2.4, a slight modification is required to the code:
replace <NUMBERFMTW> by <OS.NUMBERFMTW>.
__________

On Wednesday, January 13, 2021 at 1:22:12 AM UTC+2, F Bek wrote:
Implementing the new code, my number formatting method now reads like this:

Number >> asTextWithGrouping: groupNum groupSeparator: groupChar decimalSeparator: decimalChar decimalPlaces: decimals
   "Format the number as ###,###.## | groupNum: anInteger; groupChar: aString; decimalChar: aString; decimals: anInteger"
   ^String streamContents: [:strm | Locale default printFloat: self asFloat on: strm format: (
   NUMBERFMTW new Grouping: groupNum ; thousandSeparator: groupChar ;
   decimalSeparator: decimalChar ; decimalPlaces: decimals ;
   LeadingZero: 1 ; NegativeOrder: 1 ;
   yourself). ].

The receiver of the method is still <Number>, although it really could be any other object, and I have considered moving it to <String> instead, where it logically belongs - but that would add an argument for passing the number being formatted, so I finally decided against the change. In the above, I have added the last 2 properties (LeadingZero & NegativeOrder) more as a mental note, and for future reference, if necessary.
I then modified the converter #leftToRight method to call the new method:
FB_NumberToText >> leftToRight: aNumber
    "Answers the result of converting aNumber to a String"
    ^aNumber asTextWithGrouping: 3 groupSeparator: ',' decimalSeparator: '.' decimalPlaces: 2 .

The rest of he code remained the same. With those 2 changes, my formatter was functional again.

Now for a quick discussion - from a beginner's point of view:
Using Locale >> #printFloat:on:format: was interesting and revealing but did not really do much more than my humble converter, included in the previous message, was able to offer (except for the possibility to auto format numbers according to the Windows local default). It remains very short of a full number formatting solution, similar to Windows products, e.g. in Excel.. Again, for a programming environment that targets Windows exclusively, I thought Dolphin would offer a way to tap into all of this readily. Now it seems I have to further expand the method if I need some additional functionality - unless I am missing something again. Nevertheless, I am happy with the result so far as it fits the current need.

Thanks Sergio for the assistance, happy hear your feedback.

FB
On Saturday, January 9, 2021 at 5:08:36 PM UTC+2, F Bek wrote:
Yes! This is what I was looking for!
Experimenting with the code now...

Thanks Sergio.

FB
On Saturday, January 9, 2021 at 2:00:55 PM UTC+2, sdelf...@gmail.com wrote:
El Tuesday, January 5, 2021 a la(s) 11:48:18 AM UTC-3, fbek...@gmail.com escribió:
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.
[...]
Number>>asTextWithFormatBy: groupNum groupWith: groupChar decimalWith: decimalChar decimalNum: decimals
"Format a number as ###,###.## | groupNum: anInteger; groupChar: aChar; decimalChar: aChar; decimals: anInteger"
[...]

You should use Locale >> #printFloat:on:format::

String streamContents: [:stream |
Locale default
printFloat: 1234.56
on: stream
format: (
NUMBERFMTW new
Grouping: 3;
thousandSeparator: ',';
decimalSeparator: '.';
decimalPlaces: 3;
yourself
).
].

-- Sergio Del Franco


1
rocksolid light 0.7.2
clearneti2ptor