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 6810335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:11:38+00:00 2026-05-26T20:11:38+00:00

I know many languages have the ability to round to a certain number of

  • 0

I know many languages have the ability to round to a certain number of decimal places, such as with the Python:

>>> print round (123.123, 1)
    123.1
>>> print round (123.123, -1)
    120.0

But how do we round to an arbitrary resolution that is not a decimal multiple. For example, if I wanted to round a number to the nearest half or third so that:

123.123 rounded to nearest half is 123.0.
456.456 rounded to nearest half is 456.5.
789.789 rounded to nearest half is 790.0.

123.123 rounded to nearest third is 123.0.
456.456 rounded to nearest third is 456.333333333.
789.789 rounded to nearest third is 789.666666667.
  • 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-26T20:11:38+00:00Added an answer on May 26, 2026 at 8:11 pm

    You can round to an arbitrary resolution by simply scaling the number, which is multiplying the number by one divided by the resolution (or, easier, just dividing by the resolution).

    Then you round it to the nearest integer, before scaling it back.

    In Python (which is also a very good pseudo-code language), that would be:

    def roundPartial (value, resolution):
        return round (value / resolution) * resolution
    
    print "Rounding to halves"
    print roundPartial (123.123, 0.5)
    print roundPartial (456.456, 0.5)
    print roundPartial (789.789, 0.5)
    
    print "Rounding to thirds"
    print roundPartial (123.123, 1.0/3)
    print roundPartial (456.456, 1.0/3)
    print roundPartial (789.789, 1.0/3)
    
    print "Rounding to tens"
    print roundPartial (123.123, 10)
    print roundPartial (456.456, 10)
    print roundPartial (789.789, 10)
    
    print "Rounding to hundreds"
    print roundPartial (123.123, 100)
    print roundPartial (456.456, 100)
    print roundPartial (789.789, 100)
    

    In that above code, it’s the roundPartial function which provides the functionality and it should be very easy to translate that into any procedural language with a round function.

    The rest of it, basically a test harness, outputs:

    Rounding to halves
    123.0
    456.5
    790.0
    Rounding to thirds
    123.0
    456.333333333
    789.666666667
    Rounding to tens
    120.0
    460.0
    790.0
    Rounding to hundreds
    100.0
    500.0
    800.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how many options a drop down list can have? Is it
Many languages have an isNaN() function. I am asking myself: why check for not
I have used try-catch/except-finally variants in many languages for years, today someone asked me
In PL/SQL (or many other languages), I can have IN OUT or OUT parameters,
I'd be interested to know whether there are programming languages which have shorthand equality
I haven't done much programing in many languages, but I know in C(++), you
I have programmed many things in many programming languages, and Prolog is not one
I know many people who use computers every day, who do not know how
I know many:many isn't supported in Linq2Sql but I am working on a workaround
I would like to know many minutes between 2 dates? Example : Now -

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.