Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Truncating float numbers while displaying in Label field
Forum Updated to NodeBB v4.3 + New Features

Truncating float numbers while displaying in Label field

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 3.3k Views
  • 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.
  • O Offline
    O Offline
    o6a6r9v1p
    wrote on last edited by
    #1

    i have to display float number with 2 digits after decimal point(truncate).

    I have used
    Label_Value->setText( QString::number(value, 'f', 2));

    it is rounding a larger value while displaying the value.

    Then i used parseFloat()

    value= parseFloat(value).toFixed(2);
    Label_Value->setText( QString::number(value, 'f', 2));

    This is giving error message

    'parseFloat' was not declared in this scope.

    Searched in the net. didnot get any idea.
    What is to added/declared to use parseFloat().

    Thanks.

    jsulmJ 1 Reply Last reply
    0
    • O o6a6r9v1p

      i have to display float number with 2 digits after decimal point(truncate).

      I have used
      Label_Value->setText( QString::number(value, 'f', 2));

      it is rounding a larger value while displaying the value.

      Then i used parseFloat()

      value= parseFloat(value).toFixed(2);
      Label_Value->setText( QString::number(value, 'f', 2));

      This is giving error message

      'parseFloat' was not declared in this scope.

      Searched in the net. didnot get any idea.
      What is to added/declared to use parseFloat().

      Thanks.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @o6a6r9v1p said in Truncating float numbers while displaying in Label field:

      parseFloat

      What is parseFloat?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ToniyT Offline
        ToniyT Offline
        Toniy
        wrote on last edited by Toniy
        #3

        If you need floating value with certain precision without ceiling, try to get string longer by one symbol (three in your case) and remove all symbols that after second fractional digit:

        // Let's consider three cases:
        // 1. value == 2.65796541324 (longer than two fractional symbols);
        // 2. value == 2.6 (shorter);
        // 3. value == 2 (integer).
        QString str(QString::number(value, 'f', 3));  // 1. str == "2.658"; 2. str == "2.600"; 3. str == "2.000"
        str.chop(str.length() - (str.indexOf('.') + 3)); // 1. str == "2.65"; 2. str == "2.60"; 3. str == "2.00"
        Label_Value->setText(str);
        
        O 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          parseFloat is Javascript, not C+++

          Label_Value->setText( QString::number(std::trunc(value*100.0)/100.0, 'f', 2)); will do the trick (remember to add #include <cmath>).

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          O 1 Reply Last reply
          2
          • VRoninV VRonin

            parseFloat is Javascript, not C+++

            Label_Value->setText( QString::number(std::trunc(value*100.0)/100.0, 'f', 2)); will do the trick (remember to add #include <cmath>).

            O Offline
            O Offline
            o6a6r9v1p
            wrote on last edited by
            #5

            @VRonin

            I am mistaken about parseFloat, as you pointed out.
            modified code as you told, it is working now.

            Thank you.

            1 Reply Last reply
            0
            • ToniyT Toniy

              If you need floating value with certain precision without ceiling, try to get string longer by one symbol (three in your case) and remove all symbols that after second fractional digit:

              // Let's consider three cases:
              // 1. value == 2.65796541324 (longer than two fractional symbols);
              // 2. value == 2.6 (shorter);
              // 3. value == 2 (integer).
              QString str(QString::number(value, 'f', 3));  // 1. str == "2.658"; 2. str == "2.600"; 3. str == "2.000"
              str.chop(str.length() - (str.indexOf('.') + 3)); // 1. str == "2.65"; 2. str == "2.60"; 3. str == "2.00"
              Label_Value->setText(str);
              
              O Offline
              O Offline
              o6a6r9v1p
              wrote on last edited by
              #6

              @Toniy
              Thank you,

              1 Reply Last reply
              0

              • Login

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