'($1,925.46)' asNumber

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

'($1,925.46)' asNumber

Joseph Alotta
Greetings,

I want to convert a String with parenthesis and dollar sign to a Number or Float.

 '($1,925.46)’ asNumber

Is there something out of the box that does this, or do I have to write it myself, character by character?

Should I use a ReadStream?


Sincerely,

Joe.


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: '($1,925.46)' asNumber

Ron Teitelbaum
Hi Joe,

If you know the format is consistent and you don't really need much error handling then something like this would work

('($1,925.46)' reject: [:e | '($),' includes: e]) asNumber

All the best,

Ron Teitelbaum

> -----Original Message-----
> From: [hidden email] [mailto:beginners-
> [hidden email]] On Behalf Of Joseph Alotta
> Sent: Friday, July 01, 2016 12:41 PM
> To: [hidden email]
> Subject: [Newbies] '($1,925.46)' asNumber
>
> Greetings,
>
> I want to convert a String with parenthesis and dollar sign to a Number or
> Float.
>
>  '($1,925.46)’ asNumber
>
> Is there something out of the box that does this, or do I have to write it
> myself, character by character?
>
> Should I use a ReadStream?
>
>
> Sincerely,
>
> Joe.
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: '($1,925.46)' asNumber

Joseph Alotta
Thanks, Ron.  I needed to preserve the negative, so I append a minus sign, then use your snippet.
,

str := '($1,925.46)' .
('(*)' match: str ) ifTrue: [str := '-', str].
str := str reject:  [:e | '($,)' includes: e].
num := str asNumber  => -1925.46

Sincerely,

Joe.


Reply | Threaded
Open this post in threaded view
|

Re: '($1,925.46)' asNumber

Ron Teitelbaum

 

From: Joseph Alotta
Sent: Friday, July 01, 2016 2:59 PM

Thanks, Ron.  I needed to preserve the negative, so I append a minus sign, then use your snippet.

[Ron Teitelbaum] Excellent!  Well done!

,

str := '($1,925.46)' .
('(*)' match: str ) ifTrue: [str := '-', str].
str := str reject:  [:e | '($,)' includes: e].
num := str asNumber  => -1925.46

Sincerely,

Joe.


 


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners