#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Easy question about floats


    Im using Python 3

    How can I get a float, say 1.0 to print 1.00?
    My goal is to make it look like dollars and cents.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by looptyloop
    How can I get a float, say 1.0 to print 1.00?
    My goal is to make it look like dollars and cents.
    Code:
    >>> amount = 1
    >>> print('{0:.2f}'.format(amount))
    1.00
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by SuperOscar
    Code:
    >>> amount = 1
    >>> print('{0:.2f}'.format(amount))
    1.00
    format! thanx a bunch. Worked like a charm.

    Can you tell me how it works?
    The .2 is for two digits to the right.
    What is the 0 and f for? and is there other ways to do this?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by looptyloop
    Can you tell me how it works?
    The .2 is for two digits to the right.
    What is the 0 and f for? and is there other ways to do this?
    0 stands for the first argument in the .format() list, since counting always starts from 0 in Python, and “f” stands for “float”, since that’s how we want that argument to be interpreted.

    For further information, you’d better start from the appropriate section in the Python tutorial .
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by SuperOscar
    0 stands for the first argument in the .format() list, since counting always starts from 0 in Python, and “f” stands for “float”, since that’s how we want that argument to be interpreted.

    For further information, you’d better start from address the appropriate section in the Python tutorial .
    thankyou

IMN logo majestic logo threadwatch logo seochat tools logo