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
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
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:
Code:
#!/usr/bin/python

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 www.opentalk.org.uk 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho