| | |
Rounding to n digits
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi again i have programmed a thing that asks for 2 numbers and does a series of things to it.But i want to round the square-rooted numbers to about 6 digits e.g. 5.12345. How can this be done?
Here is my code:
Here is my code:
from math import sqrt
running = 'Y'
while 'Y' in running or 'y' in running:
#defining numbers
startn = float(raw_input('What is the starting number'))
startn2 = startn
endn = float(raw_input('What is the ending number?'))
endn2 = endn
#defining lists
num = []
nums = []
numsq = []
while startn2 != (endn2+1):
#just the numbers
a = int(startn2)
num.append(a)
startn2+=1
startn2 = startn
print 'numbers without changing are:', num
while startn2 != (endn2+1):
#numbers squared
a = int(startn2*startn2)
nums.append(a)
startn2+=1
startn2 = startn
print 'numbers squared are:', nums
while startn2 != (endn2+1):
#numbers square-rooted
#Here is my problem, i want to simplify it to about 6 digits e.g. 5.12345
a = sqrt(startn2)
numsq.append(a)
startn2+=1
startn2 = startn
print 'numbers square-rooted are:', numsq
running = raw_input('Try another set? (Y or N)') don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Built-in function round() and string formatting.
Python Syntax (Toggle Plain Text)
>>> round(math.pi, 5) 3.1415899999999999 >>> "%0.5f" % math.pi '3.14159' >>> sig_digits = 6 >>> num = 123.456789 >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num) '123.457' >>> num = 1.23456789 >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num) '1.23457' >>> num = 1234567890 >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num) '1234567890' >>>
To represent a floating point number in a formatted form for print, you have to store the result as a string in your list:
The behaviour of a floating point number you notice here is typical of many computer languages.
python Syntax (Toggle Plain Text)
# representing print formatted floats in a list list_floats = [1/3.0, 1/6.0, 3.124567, 5.01] print list_floats """ my output (note the typical float representation problem) --> [0.33333333333333331, 0.16666666666666666, 3.1245669999999999, 5.0099999999999998] """ # create a new list of floats using round() list_floats2 = [] for n in list_floats: list_floats2.append(round(n, 5)) print list_floats2 """ my output (notice we are still dealing with floats) --> [0.33333000000000002, 0.16667000000000001, 3.1245699999999998, 5.0099999999999998] """ # to make round() stick for your print # you have to store the result as a string list_floats3 = [] for n in list_floats: list_floats3.append(str(round(n, 5))) print list_floats3 """ my output --> ['0.33333', '0.16667', '3.12457', '5.01'] """ # or ... list_floats4 = [] for n in list_floats: list_floats4.append("%0.5f" % n) print list_floats4 """ my output --> ['0.33333', '0.16667', '3.12457', '5.01000'] """
Last edited by sneekula; Dec 14th, 2008 at 1:44 pm.
No one died when Clinton lied.
![]() |
Similar Threads
- rounding function with precision (C++)
- Rounding up problem (C++)
- Division Problem (C#)
- floating point : overflow error (C)
- what's with gcc? (C++)
- data format from (4.56 0.7) -> 4.6(7) awk?! (Shell Scripting)
- Rounding floating points (C)
- Number Rounding (Java)
Other Threads in the Python Forum
- Previous Thread: how to determine the period of a function
- Next Thread: Files and Dictionary Lists... Oi!
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color converter dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






