Jump to content

Conversion from string to number.


Valik
 Share

  

45 members have voted

  1. 1. Which of the following do you expect?

    • The expression Number("5abc") should return 0.
      35
    • The expression Number("5abc") should return 5.
      10


Recommended Posts

Or you could just hush and keep your opinions to yourself until you see what we actually do... in a few months.

I have developed a small game, and (co-) ran a small gaming community. The only thing the community did was 'offer suggestions', all the time, non-stop. It almost always came down to 'omhagawd please fix this'.

Just saying I know how this feels.

Edited by Manadar
Link to comment
Share on other sites

  • Replies 41
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I'd expect strings that represent a valid number to be converted to that number and an error thrown by the conversion for all other strings.

So I'd expect '5abc' to be not converted at all, neither 5 nor 0, i.e. both options don't make sense for me.

<ot>

Here's what I'm currently doing to get there:

Local $i = 0, $asResult[3] = ['hex', 'NaN', 'dec']
Local $asTest = StringSplit('-123.45e+12|-.1|23.E-9|0.|+.0|  12|001 || |0xfedcba98|-|2l0|10O0|12,345.67|9e|e8|7-6|+ 5|4.3.21|9 8|7+e6|5e4.3|2f1|1.2e-', '|')
Local $sRxNaDec = '(?i)[^-+.e\d\s]|\..*\.|e.*[^-+\d]|\A[^-+.\d\s]|[^.\d\s]\z|[^\A\s]\s[^\z\s]|[^e\A\s][-+]'
Local $sRxIsHex = '(?i)\A0x[\da-f]{1,8}\z'
For $i = 1 To $asTest[0]
    ConsoleWrite(StringFormat('%15s  %s', $asTest[$i], $asResult[2 - StringRegExp($asTest[$i], $sRxIsHex) - StringRegExp($asTest[$i], $sRxNaDec)]) & '   ''Number(…)'' = ' & Number($asTest[$i]) & @crlf)
Next
The degree of permissiveness is certainly subject to dicussion. (And in the above I don't allow thousand seperators, because I'd do so only after processing the locale settings as well.)

</ot>

Edited by memoryoverflow

(The signature is placed on the back of this page to not disturb the flow of the thread.)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...