Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:34:52+00:00 2026-05-24T00:34:52+00:00

I’ve been reading a lot of posts about rounding off numbers, but I couldn’t

  • 0

I’ve been reading a lot of posts about rounding off numbers, but I couldn’t manage to do what I want :

I have got a list of positive floats.
The unsigned integer roundOffStep to use is user-defined. I have no control other it.

I want to be able to do the most accurate rounding while preserving the sum of those numbers, or at least while keeping the new sum inferior to the original sum.

How would I do that ? I am terrible with algorithms, so this is way too tricky for me.

Thx.

EDIT : Adding a Test case :

FLOATS
   29.20
   18.25
   14.60
   8.76
   2.19

sum = 73;

Let’s say roundOffStep = 5;

ROUNDED FLOATS
30
15
15
10
0

sum = 70 < 73 OK

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:34:54+00:00Added an answer on May 24, 2026 at 12:34 am
    1. Round all numbers to the nearest multiple of roundOffStep normally.
    2. If the new sum is lower than the original sum, you’re done.
    3. For each number, calculate rounded_number - original_number. Sort this list of differences in decreasing order so that you can find the numbers with the largest difference.
    4. Pick the number that gives the largest difference rounded_number - original_number, and subtract roundOffStep from that number.
    5. Repeat step 4 (picking the next largest difference each time) until the new sum is less than the original.

    This process should ensure that the rounded numbers are as close as possible to the originals, without going over the original sum.

    Example, with roundOffStep = 5:

        Original Numbers  |   Rounded  |  Difference
    ----------------------+------------+--------------
             29.20        |     30     |     0.80
             18.25        |     20     |     1.75
             14.60        |     15     |     0.40
             8.76         |     10     |     1.24
             2.19         |     0      |    -2.19
    ----------------------+------------+--------------
    Sum:     73           |     75     |
    

    The sum is too large, so we pick the number giving the largest difference (18.25 which was rounded to 20) and subtract 5 to give 15. Now the sum is 70, so we’re done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have been unable to fix a problem with Java Unicode and encoding. The
I want to construct a data frame in an Rcpp function, but when I
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.