Decimal comma

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Decimal comma

John Toohey
Without changing the OS locale, is there an easy was to have Pharo
recognize a comma as a decimal point, i.e. '2,13' asNumber -> 2,13. I
am receiving number from a Seaside app, in this format, and I need to
convert them before storing them in Postgres. Currently '2,12'
asNumber gives 2.

--
~JT

Reply | Threaded
Open this post in threaded view
|

Re: Decimal comma

Ricardo Moran
I don't know if there is already a way of doing this, but I attached a simple hack that lets you read commas as decimal points by executing something like this:

(DecimalCommaNumberParser on: '3,14') nextNumber -> 3.14

I know this is not elegant, but it works. There seems to be some other places where the decimal point is hardcoded. Maybe we should make SqNumberParser take the decimal point as a parameter. Or have a preference to choose that. I don't know...

Cheers
Richo


On Tue, Feb 22, 2011 at 5:53 PM, John Toohey <[hidden email]> wrote:
Without changing the OS locale, is there an easy was to have Pharo
recognize a comma as a decimal point, i.e. '2,13' asNumber -> 2,13. I
am receiving number from a Seaside app, in this format, and I need to
convert them before storing them in Postgres. Currently '2,12'
asNumber gives 2.

--
~JT



DecimalCommaNumberParser.st (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Decimal comma

Ricardo Moran
Ok, ignore my last message, this new change set I'm attaching is a little nicer. It doesn't add an unnecessary SqNumberParser subclass and it lets you write something like this:

Number readFrom: '3,14' decimalPoint: $,

I also tried configuring the decimal point with a preference but the result was a little dangerous because new code using the wrong decimal point wouldn't compile, so I think this is a reasonable good solution to your problem. I hope it helps.

Cheers
Richo


On Tue, Feb 22, 2011 at 7:30 PM, Ricardo Moran <[hidden email]> wrote:
I don't know if there is already a way of doing this, but I attached a simple hack that lets you read commas as decimal points by executing something like this:

(DecimalCommaNumberParser on: '3,14') nextNumber -> 3.14

I know this is not elegant, but it works. There seems to be some other places where the decimal point is hardcoded. Maybe we should make SqNumberParser take the decimal point as a parameter. Or have a preference to choose that. I don't know...

Cheers
Richo


On Tue, Feb 22, 2011 at 5:53 PM, John Toohey <[hidden email]> wrote:
Without changing the OS locale, is there an easy was to have Pharo
recognize a comma as a decimal point, i.e. '2,13' asNumber -> 2,13. I
am receiving number from a Seaside app, in this format, and I need to
convert them before storing them in Postgres. Currently '2,12'
asNumber gives 2.

--
~JT




CommaDecimalPoint.3.cs (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Decimal comma

John Toohey
Very nice, many thanks. I'm curious as to why this has not come up
before. Any program getting data from a screen or from a file could
contain numbers in this format.

Thanks again.

On Tue, Feb 22, 2011 at 17:57, Ricardo Moran <[hidden email]> wrote:

> Ok, ignore my last message, this new change set I'm attaching is a little
> nicer. It doesn't add an unnecessary SqNumberParser subclass and it lets you
> write something like this:
> Number readFrom: '3,14' decimalPoint: $,
>
> I also tried configuring the decimal point with a preference but the result
> was a little dangerous because new code using the wrong decimal point
> wouldn't compile, so I think this is a reasonable good solution to your
> problem. I hope it helps.
> Cheers
> Richo
>
> On Tue, Feb 22, 2011 at 7:30 PM, Ricardo Moran <[hidden email]>
> wrote:
>>
>> I don't know if there is already a way of doing this, but I attached a
>> simple hack that lets you read commas as decimal points by executing
>> something like this:
>> (DecimalCommaNumberParser on: '3,14') nextNumber -> 3.14
>> I know this is not elegant, but it works. There seems to be some other
>> places where the decimal point is hardcoded. Maybe we should make
>> SqNumberParser take the decimal point as a parameter. Or have a preference
>> to choose that. I don't know...
>> Cheers
>> Richo
>>
>> On Tue, Feb 22, 2011 at 5:53 PM, John Toohey <[hidden email]> wrote:
>>>
>>> Without changing the OS locale, is there an easy was to have Pharo
>>> recognize a comma as a decimal point, i.e. '2,13' asNumber -> 2,13. I
>>> am receiving number from a Seaside app, in this format, and I need to
>>> convert them before storing them in Postgres. Currently '2,12'
>>> asNumber gives 2.
>>>
>>> --
>>> ~JT
>>>
>>
>
>



--
~JT