Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two Decimal Places
#1
Hi there,

I'm just learning python myself.

This is probably going to take someone all of two seconds to answer but I cannot find an easy answer to this anywhere.

In the following code, how do I print results to display two decimal places? - as in 123.45

#define variables with values
fastmail_payment = 2.25
agile_bits = 3.67
text_expander = 3.84
amazon_aws = 16.59
dropbox = 10.46
adobe_creative_cloud = 8.57
icloud_storage = 2.49
feedly = 3.75
google_drive = 15.99
uneasy_nostalgia = 14.40
personal_blog = 8.98




monthly_payments = fastmail_payment + agile_bits + text_expander + amazon_aws + dropbox + adobe_creative_cloud + icloud_storage + feedly + google_drive

yearly_payments = monthly_payments * 12

weekly_payments = monthly_payments / 4

daily_payments = monthly_payments / 30

hourly_payments = daily_payments / 24

squarespace_total = uneasy_nostalgia + personal_blog

squarespace_total_year = squarespace_total * 12


print "Total for Year is: " , yearly_payments

print "Total for Month is: ", monthly_payments

print "Total for Week is: ", weekly_payments

print "Total for Day is: ", daily_payments

print "Total for Hour is: ", hourly_payments

print "My Squarespace yearly total payments were: ", squarespace_total_year
Reply
#2
a = 123.456789
print "{0:.2f}".format(a)

old way
print "%.2f" % a
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Quote:daily_payments = monthly_payments / 30
what about months with 31 days?
Recommended Tutorials:
Reply
#4
(Jan-09-2018, 09:52 PM)metulburr Wrote:
Quote:daily_payments = monthly_payments / 30
what about months with 31 days?

I'm only just beginning Python. I've no idea how to deal with that.

(Jan-09-2018, 09:46 PM)Windspar Wrote:
a = 123.456789
print "{0:.2f}".format(a)

old way
print "%.2f" % a


Thanks Windspar!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculate the Euler Number with more decimal places Pedroski55 10 4,548 Oct-31-2021, 04:45 AM
Last Post: Pedroski55
  Not rounding to desired decimal places? pprod 2 2,580 Mar-05-2021, 11:11 AM
Last Post: pprod
  Can I format decimal places by column with a dictionary? Mark17 2 2,584 Dec-28-2020, 10:13 PM
Last Post: Mark17
  testing for Decimal w/o importing decimal every time Skaperen 7 4,489 May-06-2019, 10:23 PM
Last Post: Skaperen
  Search "Places near by me" or "where am I" in google maps barry76 1 2,695 Feb-07-2019, 04:10 PM
Last Post: snippsat
  Error in Python 3.6 and how to limit decimal places Raptor88 6 9,888 Mar-18-2017, 05:02 AM
Last Post: Raptor88

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020