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. Formatting floating point values as strings
QtWS25 Last Chance

Formatting floating point values as strings

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 892 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.
  • BaroneAshuraB Offline
    BaroneAshuraB Offline
    BaroneAshura
    wrote on last edited by BaroneAshura
    #1

    I am trying to be in complete control of how I am going to format a string containing a flotaing point value (be it float or double), in terms of width and precision.

    Looking at google and Qt documentaton I see three different approaches:

    • QString::asprintf : very c-oriented
    • QTextString : very c++-iostream-oriented
    • QString::arg() : lets say modern-languages-oriented

    documentation for QString::asprintf suggests avoiding it, but rather use either one or the other of the two alternatives.

    I then looked at documenation for QString::arg() , and I am very disappointed about myself for being unable to find which values are supported for the parameter format

    Am I allowed to pretend that I can pass any of the 256 values a char can have, and that there are 256 different ways to format a floating values? :P
    Is there ANY way that Qt Documentation can be considered as reliable and somewhat complete? How am I supposed to use Qt published documentation?

    Thanks in advance to anyone willing and capable of enlightnening about Qt Documentation, so far I feel totally clueless and rather helpless.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The QString::arg() documentation points to https://doc.qt.io/qt-6/qstring.html#floating-point-formats which then explains the possible values in https://doc.qt.io/qt-6/qlocale.html#toString-8

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        BTW, there is also QString::number() (with the same options as QString::arg()).

        So far, I haven't found any library that gives you full control over formatting. I'm working on some old simulation code and floating point values are stored as ASCII with 10 characters. No library (iostreams, fmt, Qt) will fully conform too just 10 characters if specified or will use a suboptimal representation (looking at you, MSVC: adding e+003 instead of just e3 to a number unnecessarily looses a lot of precision when squeezed into 10 characters). I succinctly remember iostreams with MSVC (newer versions) and fmt for some numbers to use more than 10 characters. We now have our own functions for formatting numbers based on the output of dragonbox (https://github.com/jk-jeon/dragonbox).

        JoeCFDJ 1 Reply Last reply
        0
        • S SimonSchroeder

          BTW, there is also QString::number() (with the same options as QString::arg()).

          So far, I haven't found any library that gives you full control over formatting. I'm working on some old simulation code and floating point values are stored as ASCII with 10 characters. No library (iostreams, fmt, Qt) will fully conform too just 10 characters if specified or will use a suboptimal representation (looking at you, MSVC: adding e+003 instead of just e3 to a number unnecessarily looses a lot of precision when squeezed into 10 characters). I succinctly remember iostreams with MSVC (newer versions) and fmt for some numbers to use more than 10 characters. We now have our own functions for formatting numbers based on the output of dragonbox (https://github.com/jk-jeon/dragonbox).

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          @SimonSchroeder std::format in C++ 20 may be another option.

          S 1 Reply Last reply
          1
          • JoeCFDJ JoeCFD

            @SimonSchroeder std::format in C++ 20 may be another option.

            S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            @JoeCFD said in Formatting floating point values as strings:

            std::format in C++ 20 may be another option.

            It is usually derived from the fmt library and thus has the same problems associated with it. I'm not sure about all the compiler, but because we are compiling on Windows, Linux, and macOS there will be at least one compiler (with its associated library) that is the same as the fmt library.

            1 Reply Last reply
            0
            • BaroneAshuraB BaroneAshura has marked this topic as solved on

            • Login

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