954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Rounding to n digits

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:
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)')

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

>But i want to round the square-rooted numbers to about 6 digits e.g. 5.12345. How can this be done?

Take a look at round()

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Built-in function round() and string formatting.

>>> 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'
>>>
solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

To represent a floating point number in a formatted form for print, you have to store the result as a string in your list:

# 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']
"""


The behaviour of a floating point number you notice here is typical of many computer languages.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

I was trying to make a program that gives very accurate answers to problems. The accuracy I want is of like 20000 terms.
For eg. 10/3 is written as 3.33333333333333333...20,000 times.
So, is it possible in Python?
Also, please answer in reference to Python 3.0, if possible...

techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
 

I was trying to make a program that gives very accurate answers to problems. The accuracy I want is of like 20000 terms.
For eg. 10/3 is written as 3.33333333333333333...20,000 times.
So, is it possible in Python?
The code I tried to get square root of 2 is:

import math
print(math.sqrt(2))

which gives:
1.4142135623730951... which does not fulfill my need.
Also, please answer in reference to Python 3.0, if possible...

techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
 

Doing 10/3 in 20000 decimals:

>>> base=10**20000
>>> units,decimals = divmod(10*base/3,base) ##10/3 long number
>>> print str(units)+'.'+str(decimals)


For square root you must use own function:

>>> import math
>>> x=(2*base,base)
>>> math.sqrt(x[0])

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    math.sqrt(x[0])
OverflowError: long int too large to convert to float
>>>
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Start experimenting with Python module decimal ...

# high accuracy calculations with Python module decimal
# tested with Python 3.1.2

import decimal as dc

# regular float calculation
sqrt1 = 2 ** 0.5

print(type(sqrt1))
print(sqrt1)
print(sqrt1 * sqrt1)

# set decimal precition
dc.getcontext().prec = 60

# using module decimal
sqrt2 = dc.Decimal(2) ** dc.Decimal('0.5')

print(type(sqrt2))
print(sqrt2)
print(sqrt2 * sqrt2)

"""my result -->
<class 'float'>
1.41421356237
2.0
<class 'decimal.Decimal'>
1.41421356237309504880168872420969807856967187537694807317668
2.00000000000000000000000000000000000000000000000000000000000
"""
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You