CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2001
    Posts
    165

    [RESOLVED] Rounding a number multiple of another

    Hello all,
    How would I be able to round a number in multiples of another...

    Let's say width is 150
    And multiple to be 64...
    I want 150 to become 128...
    if it was 160 to become 192...

    The width number will change and I want to covert it in multiples of the other number example 64... The minimum value will always be the multiple number used...

    Hope I explain it right...
    Thx so much...
    •SlimGradey•

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Rounding a number multiple of another

    Quote Originally Posted by SlimGradey View Post
    Hello all,
    How would I be able to round a number in multiples of another...

    Let's say width is 150
    And multiple to be 64...
    I want 150 to become 128...
    if it was 160 to become 192...

    The width number will change and I want to covert it in multiples of the other number example 64... The minimum value will always be the multiple number used...

    Hope I explain it right...
    Thx so much...
    Forget programming for a minute. If you had to do it using a pencil and paper, what would you do? What steps would you take?

  3. #3
    Join Date
    May 2001
    Posts
    165

    Re: Rounding a number multiple of another

    I was trying random things by divide multiple in half and times or add to the width value but always give wrong thing so I tried to hard code it
    By doing if width less than 96 width equal 64 and if greater than 96 width is 128... And if less than 160 then width is 128 and if greater than 160 width is 192...and so on till over 512... But think my if statements all nested wrong...

    Be better if didn't need to hard code it... I'm still new to math type functions... Not sure how to use % ^ ~ things...
    •SlimGradey•

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Rounding a number multiple of another

    Code:
    int tonearestmultiple(int value, int roundto)
    {
        return   ( (value + (roundto/2)) / roundto ) * roundto;
    }
    You may have to adjust the above formula depending on how you expect it to work for negative numbers.
    Depending on expectations for odd 'roundto' values, you may also have to adjust the +(roundto/2) part to correspond to your needs.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Rounding a number multiple of another

    Once you have worked out the logic, this can be done in 2 lines of code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Rounding a number multiple of another

    Quote Originally Posted by SlimGradey View Post
    I was trying random things by divide multiple in half and times or add to the width value but always give wrong thing so I tried to hard code it
    By doing if width less than 96 width equal 64 and if greater than 96 width is 128... And if less than 160 then width is 128 and if greater than 160 width is 192...and so on till over 512... But think my if statements all nested wrong...

    Be better if didn't need to hard code it... I'm still new to math type functions... Not sure how to use % ^ ~ things...
    This isn't about math or %^~ things, it's about logic, and trying random things is never a good idea. You can't write a program if you don't understand how to solve the problem, so working out the steps is the first thing to do.

  7. #7
    Join Date
    May 2001
    Posts
    165

    Re: Rounding a number multiple of another

    Quote Originally Posted by OReubens View Post
    Code:
    int tonearestmultiple(int value, int roundto)
    {
        return   ( (value + (roundto/2)) / roundto ) * roundto;
    }
    You may have to adjust the above formula depending on how you expect it to work for negative numbers.
    Depending on expectations for odd 'roundto' values, you may also have to adjust the +(roundto/2) part to correspond to your needs.
    Thx so much guys... I was so close the first time... I had roundto only twice and not three times... Finally its working... Appreciate so much...
    •SlimGradey•

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Rounding a number multiple of another

    Quote Originally Posted by SlimGradey View Post
    I was trying random things ...
    That could take awhile

    Quote Originally Posted by SlimGradey View Post
    I'm still new ...
    How new could you be? You are on this forum for 12+ years!
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured