Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How does Rounding work in Python?
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Planet_Fall




PostPosted: Tue Jan 21, 2014 7:15 pm   Post subject: How does Rounding work in Python?

I have statistic homework, and I was making a program to find the class intervals but the final answer needs to be rounded up to the nearest 10 or 5. How would I do this?

Python:


array[]
y = 2
# Gets number of pieces of information
number_info = int(input("How many items of information do you have? "))

# Gets number of classes
def number_of_classes (x):
    higher = False
    while higher == False:
        K = pow(2,y)
        if K >= number_info:
            higher = True
        else:
            y += 1
    return "There are", y , "classes"

# Gets pieces of information
for i in range (0, number_info):
    array.append(float(input("Enter Info: ")))


# Gets Lowest and Highest Number
number_comp_A = array[0]
number_comp_B = array[0]
for i in range (0, number_info):
    number_to_comp_lowest = array[i]
    number_to_comp_highest = array[i]
    if number_comp_A > number_to_comp_lowest:
        number_comp_A = number_to_comp_lowest
    if number_comp_B < number_to_comp_highest: 
        number_comp_B = number_to_comp_highest
highest_num = number_comp_B
lowest_num = number_comp_A

# Finds Class Intervals
# i >= H - L / Classes
number_of_classes (number_info)
intervals = (highest_num - lowest_num) / y
print (intervals)
print (highest_num)
print (lowest_num)
print (y)

Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Tue Jan 21, 2014 8:34 pm   Post subject: Re: How does Rounding work in Python?

You can use the round function to round floating point numbers to any number of digits after the decimal point (you can use negative numbers to round to digits before the decimal point).

So this lets you round to the nearest multiple of any power of ten you want. To round to the nearest multiple of any other number just divide by this number, round, then multiply by the number.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: