Python
  Home arrow Python arrow Page 3 - Python: Count on It
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python: Count on It
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-19

    Table of Contents:
  • Python: Count on It
  • Decimals, Division, and Floats
  • Floating Points
  • Built-in Math Functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Anyone looking for a way to modernize legacy data or easily migrate to a more cost-effective database without sacrificing functionality will benefit from this seminar. View the Intro to Advantage Database Server now!

    Python: Count on It - Floating Points
    (Page 3 of 4 )

    As we discussed way back in one of the beginning tutorials on Python, a floating point refers to a decimal value. Floating points in Python have a size limit of 17 decimal digits, the largest being 1.79769_10308. Any number larger will result in an error, or a line that says: inf (standing for infinity).

    Let's take a look at the following float value:


    >>> 4.1*9

    36.899999999999999

    That's a pretty crazy number of digits to the right of the decimal. What if we don't want to see those? We can format our floating points using the str() function, which allows us to display the number as a string:


    >>> 4.1*9

    36.899999999999999

    >>> str(4.1*9)

    '36.9'

    You can also use the round function to round out the number, like so:


    >>> round(4.1*9)

    37.0

    But what if you have a number such as 52.9 and you want it to return 52.0 instead of 53.0, since technically, it isn't 53.0. Try using round first:

    >>> round(52.9)

    53.0

    As you can see, round makes it 53.0 automatically. So what do we do if we want it to show 52.0 instead? We use the floor module, that's what:


    >>> import math

    >>> math.floor(52.9)

    52.0

    Voila! Easy as pie. That you buy in a supermarket and don't actually make yourself.

    Of course, if you are in the reverse situation, you can use our pal, the ceil module (short for ceiling) which does the opposite of the floor module:


    >>> import math

    >>> math.ceil(53.1)

    54.0

    If you want to control how many decimals are displayed, you can use %f to format it. Let's say you want to see the number 19 as a decimal. Try typing in the following code:


    >>> print "%f" % 19

    19.000000

    As you can see, it has taken your integer, 19, and formatted it as a decimal, giving the result 19.000000. But what if you only wanted two decimal places? Try this:


    >>> print "%.2f" % 19

    19.00

    If you had typed in %.3f, you would have gotten this result:

    >>> print "%.3f" %19

    19.000

    and so forth, up until %.68f.

    More Python Articles
    More By James Payne


       · Hey, thanks for stopping by. In this article we cover math in Python, including such...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway