Python Forum

Full Version: Two Decimal Places
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
a = 123.456789
print "{0:.2f}".format(a)

old way
print "%.2f" % a
Quote:daily_payments = monthly_payments / 30
what about months with 31 days?
(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!