[SOLVED] parseInt problem...



  • Hello,
    I'm using parseInt in my program.
    It works fine for the strings not starting with zero like, "1234"...
    But for strings starting with zero, say "0987", it returns zero...

    why is it like this.? shouldn't it return the actual value.??


  • Moderators

    Hm, I think that parseInt is not part of QML itself, but of JS? In that case, please add which Qt version are you using. It might be a bug.


  • Moderators

    I don't know QML or its parseInt implementation too well, but usually numbers starting with 0 are considered to be octal (base 8). Only the digits 0-7 make sense there, so the longest possible sequence of digits that can be converted is 0.

    Try 0765 and check whether that is correctly interpreted as decimal 501.



  • [quote author="Tobias Hunger" date="1340001735"]I don't know QML or its parseInt implementation too well, but usually numbers starting with 0 are considered to be octal (base 8). Only the digits 0-7 make sense there, so the longest possible sequence of digits that can be converted is 0.[/quote]

    This is exactly what's happening. You can force the number to be interpreted in base 10 like so:
    @parseInt("09876", 10);@
    or you can use the Number function:
    @Number("09876");@

    Both should get you what you're looking for. Note that I have not tried either solution in QML/JS, I am simply quoting this page:
    http://stackoverflow.com/questions/850341/workarounds-for-javascript-parseint-octal-bug
    (Also, to be fair, the title of that page is poorly worded -- this is not a bug)



  • yes, it was my mistake that i didn't checked the details.
    by specifying "radix", one can force parseInt for conversion...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.