Python
  Home arrow Python arrow Page 3 - Python: Count on It
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 2
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT