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. [solved] Roundup value to nearest 200
Forum Updated to NodeBB v4.3 + New Features

[solved] Roundup value to nearest 200

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.4k 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.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    Hi,
    I need to roundup a value which is always a positive integer. I have to round up it to the nearest 200.
    Rules are;

    1. Value should be rounded-up to nearest 200. (if 350 I need 400 and if 250 I need 200)
    2. Minimum value should be 200. (even 1 should give me 200)

    Can anybody help me?

    Thanks in advance.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      use the modulo operator "%".
      if the value returned by modulo is smaller than 100 (half of 200) subtract the mod-value from the current value, otherwise add (200-moduloValue) to your current value.

      You just need a special case handling for 2)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hareen Laks
        wrote on last edited by
        #3

        raven-worx,

        Thanks for replying.

        I created below method. Can you check is it the best way for do that?

        @void setLength(int l)
        {
        int minimum_length = 200;

        if(l<minimum_length)
        {
            l=minimum_length;
        }
        else
        {
            int mod_200 =l % 200 ;
        
            if(mod_200!=0)
            {
                l = (l/200 + qRound(mod_200/100))*200;
            }
        }
        
        d->length = l;
        

        }@

        Thanks again.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          have you tested your method?!

          Your method doesn't behave like you described it should be.
          Neither did you implement what i suggested.

          Here is the working method:
          @
          int roundValue(int l)
          {
          const int mod = 200;

          if(l < mod)
          {
              l = mod;
          }
          else
          {
              int modVal = l % mod;
          
              if( modVal < (mod/2.0) )
                  l -= modVal;
              else
                  l += (mod-modVal);
          }
          
          return l;
          

          }
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hareen Laks
            wrote on last edited by
            #5

            yes I tested it and it is working for me. :)

            give minimum 200 and round value to nearest 200.

            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