Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] parseInt problem...
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] parseInt problem...

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 4 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aekam
    wrote on 18 Jun 2012, 04:42 last edited by
    #1

    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.??

    If you take care of inches, you won't have to worry about miles... :)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 18 Jun 2012, 05:32 last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on 18 Jun 2012, 06:42 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DigitalPioneer
          wrote on 18 Jun 2012, 14:15 last edited by
          #4

          [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)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aekam
            wrote on 19 Jun 2012, 07:02 last edited by
            #5

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

            If you take care of inches, you won't have to worry about miles... :)

            1 Reply Last reply
            0

            1/5

            18 Jun 2012, 04:42

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved