Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 12:01 AM
looptyloop looptyloop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 looptyloop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 19 sec
Reputation 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.

Reply With Quote
  #2  
Old November 15th, 2012, 04:14 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 1 m 41 sec
Reputation Power: 65
Quote:
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #3  
Old November 15th, 2012, 12:20 PM
looptyloop looptyloop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 looptyloop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 19 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #4  
Old November 15th, 2012, 12:33 PM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 1 m 41 sec
Reputation Power: 65
Quote:
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 .

Reply With Quote
  #5  
Old November 15th, 2012, 12:44 PM
looptyloop looptyloop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 looptyloop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 19 sec
Reputation Power: 0
Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Easy question about floats

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap