Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 6 Online
OpenStudy (anonymous):

How do i use round(number[,]) function? need it for pset2

OpenStudy (microbot):

Well lets say your number is: longfloat=123.364573648538 You want it to print out max 2 floating point digits (rounding the rest of the digits) : 123.36 To do that you must type : round(longfloat,2) where, longfloat is the variable where you assign your number and 2 the length of the floating point part. Try it in Python by changing 2 and putting other numbers to see how it works:)

OpenStudy (anonymous):

Ok, this works perfectly in the shell, but in a script it seems to have no effect. How can this be?

OpenStudy (microbot):

how do u write it in the script?

OpenStudy (microbot):

in the script to print out smthing u must use the print function . so: print (round(longfloat,2))

OpenStudy (anonymous):

tried that, it prints but its not rounded

OpenStudy (anonymous):

Do you mean rounded to the nearest integer or rounded to specific decimal places? @MicroBot showed you how to round to specific decimal places!

OpenStudy (microbot):

@snackbox can u paste the code line that u r trying to print out so we can help u more?

OpenStudy (anonymous):

Thanks @MicroBot here is the code. I'd like to round output 1, and output 2, to two decimal places.

OpenStudy (anonymous):

x = 1000 y = 0.18 z = 0.02 m = 1 while m < 13: print 'period : ', m mp = x * z print 'output 1 : ', mp rb = (x - mp) * (1 + z / 12) print 'output 2 : ', rb m = m + 1 x = rb

OpenStudy (microbot):

well u r supposed to write: print ('output 1:', round(mp,2)) same procedure for output 2. tell me if it works.

OpenStudy (anonymous):

Taa Daa! It worked. You da bomb. take a bow

OpenStudy (microbot):

xD im happy it did!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!