Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Printing out currencies in Python

Posted by enquirer (enquirer), 23 January 2004
How do I print out the number contained in a float object in Python to a certain number of decimal places?   How do I format a time so that if I have an hour of 7 and a minute of 9 it comes out as 07:09?

Posted by admin (Graham Ellis), 23 January 2004
In Python, the % operator is overloaded - it gives "remainder when divided by" if the parameter to the left of the % character is a numeric object type, and it gives "formatted to" if the parameter to the left of the % character is a string.

In other words, the % operator in python is like the sprintf function in Perl, PHP and C.

Sample program:

vatrate = 17.5

amount = float(raw_input("Please enter an amount paid: "))
hour = int(raw_input("What was the hour? "))
min = int(raw_input("What was the minute? "))

result = "The amount %.2f was paid at %02d:%02d" % (amount, hour, min)
tax = amount * vatrate / (100.0 + vatrate)
taxinfo = "That's %.2f plus %.2f tax" % (amount -tax, tax)

print result
print taxinfo

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho