String to number conversion issues

classic Classic list List threaded Threaded
3 messages Options
larrry larrry
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

String to number conversion issues

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.
Sven Van Caekenberghe Sven Van Caekenberghe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: String to number conversion issues

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
larrry larrry
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: String to number conversion issues



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.   

Loading...