Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Variable Problem

    General and Desktop
    2
    3
    1364
    Loading More Posts
    • 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.
    • K
      kingsta last edited by

      Hello guys.

      I have a little problem. My program reads numbers from txt file and show.

      data.txt
      @12345,67e-09@

      code.cpp
      @
      ...
      QFile file(fileName);
      QTextStream in(&file);

      double textData;
      QString data;

      in >> data;

      textData = data.toDouble();
      @

      It works. But when i change data.txt, there is a problem.

      data.txt
      @1234567e-09@

      Program shows the number that is 0,00123457. I want to show 1234567 x 10^9 ( = 1234567 x 1000000000 ).

      I guess The problem occur because of double variable type. How can i fix this?

      1 Reply Last reply Reply Quote 0
      • M
        MuldeR last edited by

        [quote author="kingsta" date="1364855711"]@1234567e-09@

        Program shows the number that is 0,00123457. I want to show 1234567 x 10^9 ( = 1234567 x 1000000000 ).[/quote]

        Why?

        If the file contains "1234567e-09", which according to the "scientific notation":http://en.wikipedia.org/wiki/Scientific_notation means "1234567 × 10^(-9)" and not "1234567 × 10^9". The latter would be "1234567e09" instead of "1234567e-09".

        But if you want the text to be interpreted differently from the "usual" commonly-accepted conventions, you will have to read it as QString and then implement your own "non-standard" parser logic...

        --

        BTW: As plain text always is ambiguous (unless both sides agree an a strict convention), you should better use a QDataStream. This class provides well-defined and platform-independent encodings for all common data types.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply Reply Quote 0
        • K
          kingsta last edited by

          [quote author="MuldeR" date="1364861518"][quote author="kingsta" date="1364855711"]@1234567e-09@

          Program shows the number that is 0,00123457. I want to show 1234567 x 10^9 ( = 1234567 x 1000000000 ).[/quote]

          Why?

          If the file contains "1234567e-09", which according to the "scientific notation":http://en.wikipedia.org/wiki/Scientific_notation means "1234567 × 10^(-9)" and not "1234567 × 10^9". The latter would be "1234567e09" instead of "1234567e-09".

          But if you want the text to be interpreted differently from the "usual" commonly-accepted conventions, you will have to read it as QString and then implement your own "non-standard" parser logic...

          --

          BTW: As plain text always is ambiguous (unless both sides agree an a strict convention), you should better use a QDataStream. This class provides well-defined and platform-independent encodings for all common data types.[/quote]

          Thanks for help. Yes you right I was wrong. I am going to look QDataStream.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post