String to number conversion issues

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

String to number conversion issues

larrry
Hi,

I need to convert web input (Strings) to decimal numbers. I have a regex that will recognize numeric strings, but not all of them can be converted to a number with asNumber.  For example, anything with a leading $+ fails, as does anything beginning with '.'  (even though .3 isNumber answers true.) or '-.'

Is there any code for dealing with this kind of thing (ie a method to be called before calling asNumber or a more robust library for string conversions? Given that all web input is strings and a lot of it is decimal numbers, i have to believe there is code somewhere that does this safely, maybe even with tests. :)  

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: String to number conversion issues

Sven Van Caekenberghe
Hi Larry,

On 18 Sep 2011, at 21:37, Larry White wrote:

> Hi,
>
> I need to convert web input (Strings) to decimal numbers. I have a regex that will recognize numeric strings, but not all of them can be converted to a number with asNumber.  For example, anything with a leading $+ fails, as does anything beginning with '.'  (even though .3 isNumber answers true.) or '-.'
>
> Is there any code for dealing with this kind of thing (ie a method to be called before calling asNumber or a more robust library for string conversions? Given that all web input is strings and a lot of it is decimal numbers, i have to believe there is code somewhere that does this safely, maybe even with tests. :)  
>
> Thanks.

Maybe Number class>>#readFrom:ifFail: would be helpful ?

Sven
Reply | Threaded
Open this post in threaded view
|

Re: String to number conversion issues

larrry


On Sun, Sep 18, 2011 at 4:36 PM, Sven Van Caekenberghe <[hidden email]> wrote:
Hi Larry,

On 18 Sep 2011, at 21:37, Larry White wrote:

> Hi,
>
> I need to convert web input (Strings) to decimal numbers. I have a regex that will recognize numeric strings, but not all of them can be converted to a number with asNumber.  For example, anything with a leading $+ fails, as does anything beginning with '.'  (even though .3 isNumber answers true.) or '-.'
>
> Is there any code for dealing with this kind of thing (ie a method to be called before calling asNumber or a more robust library for string conversions? Given that all web input is strings and a lot of it is decimal numbers, i have to believe there is code somewhere that does this safely, maybe even with tests. :)
>
> Thanks.

Maybe Number class>>#readFrom:ifFail: would be helpful ?

Sven

Thank you. When I was looking for that method, I found the class ExtendedNumberParser, which handles all the cases mentioned properly.