How do i use round(number[,]) function? need it for pset2
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:)
Ok, this works perfectly in the shell, but in a script it seems to have no effect. How can this be?
how do u write it in the script?
in the script to print out smthing u must use the print function . so: print (round(longfloat,2))
tried that, it prints but its not rounded
Do you mean rounded to the nearest integer or rounded to specific decimal places? @MicroBot showed you how to round to specific decimal places!
@snackbox can u paste the code line that u r trying to print out so we can help u more?
Thanks @MicroBot here is the code. I'd like to round output 1, and output 2, to two decimal places.
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
well u r supposed to write: print ('output 1:', round(mp,2)) same procedure for output 2. tell me if it works.
Taa Daa! It worked. You da bomb. take a bow
xD im happy it did!
Join our real-time social learning platform and learn together with your friends!