• string to integer?

    From john c@21:1/5 to All on Mon Oct 25 13:00:50 2021
    hi all,

    i'm trying to convert a string to an integer. the string is '+7' (43, 55). so i thought Integer fromString: aString would be the answer. not so easy. it fails with an 'Invalid format for Integer' . not sure why it's an invalid format, but be that as it
    may, what are my options? just for grins, '-7' --> -7 works just fine, so what is it with '+'?

    thanks in advance for help,

    john

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vinref@gmail.com@21:1/5 to john c on Mon Oct 25 16:59:04 2021
    On Tuesday, 26 October 2021 at 06:00:51 UTC+10, john c wrote:
    hi all,

    i'm trying to convert a string to an integer. the string is '+7' (43, 55). so i thought Integer fromString: aString would be the answer. not so easy. it fails with an 'Invalid format for Integer' . not sure why it's an invalid format, but be that as it
    may, what are my options? just for grins, '-7' --> -7 works just fine, so what is it with '+'?

    thanks in advance for help,

    john
    Integer class>>#readFrom: only ever checks for the existence of negative sign to signify a negative number, not the positive sign to signify a positive number. I guess a number without a positive sign is always considered a positive number and the
    presence of the positive sign is seen as an error.

    You will have to strip the positive sign (aString reject: [ :each | each = $+ ])

    Vince

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From john c@21:1/5 to vin...@gmail.com on Mon Oct 25 17:58:17 2021
    On Monday, October 25, 2021 at 7:59:05 PM UTC-4, vin...@gmail.com wrote:
    On Tuesday, 26 October 2021 at 06:00:51 UTC+10, john c wrote:
    hi all,

    i'm trying to convert a string to an integer. the string is '+7' (43, 55). so i thought Integer fromString: aString would be the answer. not so easy. it fails with an 'Invalid format for Integer' . not sure why it's an invalid format, but be that as
    it may, what are my options? just for grins, '-7' --> -7 works just fine, so what is it with '+'?

    thanks in advance for help,

    john
    Integer class>>#readFrom: only ever checks for the existence of negative sign to signify a negative number, not the positive sign to signify a positive number. I guess a number without a positive sign is always considered a positive number and the
    presence of the positive sign is seen as an error.

    You will have to strip the positive sign (aString reject: [ :each | each = $+ ])

    Vince
    thanks vince,

    i arrived at that same conclusion and i'm modifying as we speak. with that said, it strikes me that a valid format should be accepted rather than putting on every developer.

    sorry, just ranting.

    john

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From john.aspinall@gmail.com@21:1/5 to john c on Tue Oct 26 05:42:34 2021
    Hi John - the comment for Number class>>readFrom: states

    "Any numbers in the stream are expected to obey Smalltalk syntax rather than any locale specific formatting, i.e. this is not intended for reading user input."

    ...so bearing this in mind the error is correct since +7 is not valid Smalltalk syntax.

    I thought that the NumberToText class would handle your scenario since it's part of the MVP framework and thus potentially user-facing. However it also uses Number class>>readFrom: and so has the same problem (though it does at least have a #todo comment
    on the reverse conversion saying "...should use GetNumberFormat() to produce locale specific output").



    On Tuesday, October 26, 2021 at 1:58:18 AM UTC+1, john c wrote:
    On Monday, October 25, 2021 at 7:59:05 PM UTC-4, vin...@gmail.com wrote:
    On Tuesday, 26 October 2021 at 06:00:51 UTC+10, john c wrote:
    hi all,

    i'm trying to convert a string to an integer. the string is '+7' (43, 55). so i thought Integer fromString: aString would be the answer. not so easy. it fails with an 'Invalid format for Integer' . not sure why it's an invalid format, but be that
    as it may, what are my options? just for grins, '-7' --> -7 works just fine, so what is it with '+'?

    thanks in advance for help,

    john
    Integer class>>#readFrom: only ever checks for the existence of negative sign to signify a negative number, not the positive sign to signify a positive number. I guess a number without a positive sign is always considered a positive number and the
    presence of the positive sign is seen as an error.

    You will have to strip the positive sign (aString reject: [ :each | each = $+ ])

    Vince
    thanks vince,

    i arrived at that same conclusion and i'm modifying as we speak. with that said, it strikes me that a valid format should be accepted rather than putting on every developer.

    sorry, just ranting.

    john

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)