Get float number from string?

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

Get float number from string?

lhhuong
This post was updated on .
Hi everybody, I have a program to get data from text file.
I got a string with format as example: "9.7087988 @ 105.51043623333334 17421.193339188103"
I want to get 3 float number and store it into 3 variables:
   number1: 9.7087988
   number2: 105.51043623333334
   number3: 17421.193339188103
What should I do?
Help me, please
Reply | Threaded
Open this post in threaded view
|

Re: Get float number from string?

Paul Baumann
Here is one way:

        | string |
        string := '9.7087988 @ 105.51043623333334 17421.193339188103'.
        (string runsFailing: [:c | c isSeparator or: [c == $@]])
                collect: [:ea | (ea, 'd') asNumber ]

         OrderedCollection (9.7087988d 105.51043623333d 17421.193339188d)

Here is another:

        | rs oc |
        rs := '9.7087988 @ 105.51043623333334 17421.193339188103' readStream.
        oc := OrderedCollection new.
        [rs atEnd] whileFalse: [
                rs peek isDigit
                ifTrue: [oc add: (Double readFrom: rs)]
                ifFalse: [rs next]
        ].
        oc

         OrderedCollection (9.7087988d 105.51043623333d 17421.193339188d)


Paul Baumann

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of lhhuong
Sent: Thursday, June 19, 2014 22:18
To: [hidden email]
Subject: [vwnc] Get float number from string?

Hi everybody, I have a program to get data from text file.
I got a string with format as example: "9.7087988 @ 105.51043623333334 17421.193339188103"
I want to get 3 float number and store it into 3 variables:
   number1: 9.7087988
   number2: 105.51043623333334
   number3: 17421.193339188103
What should I do?
Help me, please



--
View this message in context: http://forum.world.st/Get-float-number-from-string-tp4763901.html
Sent from the VisualWorks mailing list archive at Nabble.com.
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Get float number from string?

Paul Baumann
In reply to this post by lhhuong
When the data format is predictable (and it is OK to error when it is not) then some people use hard-coded reading like this:

        | rs number1 number2 number3 |
        rs := '9.7087988 @ 105.51043623333334 17421.193339188103' readStream.
        number1 := Double readFrom: rs.
        rs next: 3.
        number2 := Double readFrom: rs.
        rs next.
        number3 := Double readFrom: rs.
        Array with: number1 with: number2 with: number3

         #(9.7087988d 105.51043623333d 17421.193339188d)

What you use would likely be some variant of the three examples.



-----Original Message-----
From: Paul Baumann
Sent: Friday, June 20, 2014 09:58
To: 'lhhuong'; [hidden email]
Subject: RE: [vwnc] Get float number from string?

Here is one way:

        | string |
        string := '9.7087988 @ 105.51043623333334 17421.193339188103'.
        (string runsFailing: [:c | c isSeparator or: [c == $@]])
                collect: [:ea | (ea, 'd') asNumber ]

         OrderedCollection (9.7087988d 105.51043623333d 17421.193339188d)

Here is another:

        | rs oc |
        rs := '9.7087988 @ 105.51043623333334 17421.193339188103' readStream.
        oc := OrderedCollection new.
        [rs atEnd] whileFalse: [
                rs peek isDigit
                ifTrue: [oc add: (Double readFrom: rs)]
                ifFalse: [rs next]
        ].
        oc

         OrderedCollection (9.7087988d 105.51043623333d 17421.193339188d)


Paul Baumann

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of lhhuong
Sent: Thursday, June 19, 2014 22:18
To: [hidden email]
Subject: [vwnc] Get float number from string?

Hi everybody, I have a program to get data from text file.
I got a string with format as example: "9.7087988 @ 105.51043623333334 17421.193339188103"
I want to get 3 float number and store it into 3 variables:
   number1: 9.7087988
   number2: 105.51043623333334
   number3: 17421.193339188103
What should I do?
Help me, please



--
View this message in context: http://forum.world.st/Get-float-number-from-string-tp4763901.html
Sent from the VisualWorks mailing list archive at Nabble.com.
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Get float number from string?

lhhuong
In reply to this post by lhhuong
Ok, thank you very much.