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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:51:26+00:00 2026-05-31T01:51:26+00:00

I would like to write something which looks a bit like what I currently

  • 0

I would like to write something which looks a bit like what I currently have in the following definition:

def rounding( sp1, sp2, sp3, sp4 ):

    if () <= 700.:
        round(object, -1)
    elif () <= 1000.:
        round(object, (-1*5))
    elif () <= 10000.:
        round(object, -2)
    elif () > 10000.:
        round(object, -3)
    else:
        print "Error"

    return [ sp1, sp2, sp3, sp4 ]

I am really a beginner to all of this and I’ve probably made some horrendous mistakes in the above code. What I basically have is previously calculated these 4 values sp1, sp2 etc. I then want to be able to round these values all at the same time but depending on their individual value, they must be rounded by different amounts.

If 'sp' <= 700 : rounded to nearest 10
700 < sp <= 1000 : rounded to nearest 50
1000 < sp <= 10000 : rounded to nearest 100
10000 < sp : rounded to nearest 1000. (not code)

If something could help me I would be very grateful, thank you!

  • 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-31T01:51:27+00:00Added an answer on May 31, 2026 at 1:51 am

    The tricky part is rounding to the nearest 50. That can be achieved by doubling the number, then rounding to the nearest 100, then halving the number again. At least that’s the easiest way I can think of.

    def nearest_50(n):
        return round(n*2, -2)/2
    

    As for the rest, your code is not that far away from a correct solution. As Ignacio suggested, you should only round one value at a time

    def my_round(n):
        if n <= 700:
            return round(n, -1)
        elif n <= 1000:
            return nearest_50(n)
        elif n <= 10000:
            return round(n, -2)
        else:
            return round(n, -3)
    

    Then round a list of numbers like this

    mylist = [... some numbers ...]
    rounded = [my_round(n) for n in mylist] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am working in VC++ 2008 (express) and I would like to write something in
I would like to write an if statement that will do something base on
Would you write something like: enum XYZ_TYPE {X=1, Y=2, Z=3}; I saw it and
I would like to write a small program in C# which goes through my
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have a Excel Spreadsheet with lab data which looks like this: µg/L (ppb)
I have a bunch of Repository classes which all look a bit like the
I would like to write some data to a file in Ruby. What is
I would like to write a plug-in that will allow a custom written CRM
I would like to write my own OS, and would like to temporarily jump

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.