Rounding to n digits

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 402
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Pro in Training

Rounding to n digits

 
0
  #1
Dec 13th, 2008
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)')
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,039
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Rounding to n digits

 
0
  #2
Dec 13th, 2008
>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()
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Rounding to n digits

 
0
  #3
Dec 13th, 2008
Built-in function round() and string formatting.
  1. >>> round(math.pi, 5)
  2. 3.1415899999999999
  3. >>> "%0.5f" % math.pi
  4. '3.14159'
  5. >>> sig_digits = 6
  6. >>> num = 123.456789
  7. >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
  8. '123.457'
  9. >>> num = 1.23456789
  10. >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
  11. '1.23457'
  12. >>> num = 1234567890
  13. >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
  14. '1234567890'
  15. >>>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,284
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Rounding to n digits

 
0
  #4
Dec 14th, 2008
To represent a floating point number in a formatted form for print, you have to store the result as a string in your list:
  1. # representing print formatted floats in a list
  2.  
  3. list_floats = [1/3.0, 1/6.0, 3.124567, 5.01]
  4.  
  5. print list_floats
  6.  
  7. """
  8. my output (note the typical float representation problem) -->
  9. [0.33333333333333331, 0.16666666666666666, 3.1245669999999999, 5.0099999999999998]
  10. """
  11.  
  12. # create a new list of floats using round()
  13. list_floats2 = []
  14. for n in list_floats:
  15. list_floats2.append(round(n, 5))
  16.  
  17. print list_floats2
  18.  
  19. """
  20. my output (notice we are still dealing with floats) -->
  21. [0.33333000000000002, 0.16667000000000001, 3.1245699999999998, 5.0099999999999998]
  22. """
  23.  
  24. # to make round() stick for your print
  25. # you have to store the result as a string
  26. list_floats3 = []
  27. for n in list_floats:
  28. list_floats3.append(str(round(n, 5)))
  29.  
  30. print list_floats3
  31.  
  32. """
  33. my output -->
  34. ['0.33333', '0.16667', '3.12457', '5.01']
  35. """
  36.  
  37. # or ...
  38. list_floats4 = []
  39. for n in list_floats:
  40. list_floats4.append("%0.5f" % n)
  41.  
  42. print list_floats4
  43.  
  44. """
  45. my output -->
  46. ['0.33333', '0.16667', '3.12457', '5.01000']
  47. """
The behaviour of a floating point number you notice here is typical of many computer languages.
Last edited by sneekula; Dec 14th, 2008 at 1:44 pm.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC