Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. how to convert double number to quint16
Forum Updated to NodeBB v4.3 + New Features

how to convert double number to quint16

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 5.3k 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.
  • T Offline
    T Offline
    ThenewQtDev
    wrote on last edited by kshegunov
    #1

    i have quint number multiply to 3.6067688
    and i want to get the quint result
    i know it is easy but really it took time from me
    and i couldnt solve it

    [Moved to the proper forum ~kshegunov]

    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      https://doc.qt.io/qt-5/qtglobal.html#qRound

      Or:

      double x = 3.6;
      quint32 l = quint32(x + 0.5);

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • 6thC6 Offline
        6thC6 Offline
        6thC
        wrote on last edited by
        #3

        @kshegunov said in how to convert double number to quint16:

        quint32

        I'm sure you guys know what you are doing, I just always get red flags and start to questioning / confirming I do mean to use unsigned - Just thinking aloud about potentials:

        1:  using quint32 = unsigned int;
        2:  x= std::numeric_limits<unsigned int>::max();
        3:  x + 0.5
        4:  ?lost profit?
        kshegunovK 1 Reply Last reply
        0
        • 6thC6 6thC

          @kshegunov said in how to convert double number to quint16:

          quint32

          I'm sure you guys know what you are doing, I just always get red flags and start to questioning / confirming I do mean to use unsigned - Just thinking aloud about potentials:

          1:  using quint32 = unsigned int;
          2:  x= std::numeric_limits<unsigned int>::max();
          3:  x + 0.5
          4:  ?lost profit?
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          If you want to handle the overflow you need to handle it manually anyway. There's no way to signal that a conversion has failed by the result itself, so the be completely proper you'd need to check if the double you're converting from is over the max value of the resulting type minus 0.5 and if so you're going to overflow.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2

          • Login

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