Re: [Tutor] Help - want to display a number with two decimal places

2011-03-05 Thread Steven D'Aprano

Modulok wrote:


Notice that 'd' in your string substitution means integers, not
floats. Any decimal places will be truncated when using 'd'. For
floats use 'f' instead. You an also specify a precision, such as
'%.2f' shows two decimal places.

However, to make all of your numbers line up nicely, regardless of how
long they are, you need to look into advanced string formatting via
the builtin string method 'format()'. It takes a little practice to
understand and use, but the results are exactly what you want.


str.format only exists from Python 2.6, but in any case, it certainly 
isn't true that you "need" to look at format to make numbers line up 
nicely. The examples you give can all be written using string interpolation:



print "{0:<18} {1:>18.2f}".format("Country Sales Tax", countrySalesTax)
print "{0:<18} {1:>18.2f}".format("Purchase Price", purchasePrice)
# First prints the first argument (the 0th index) to 'format(),
# left aligned '<', and 18 characters wide, whitespace padded.
# Then prints the second argument, (the 1st index) right aligned,
# also 18 characters wide, but the second argument is specified to
# be a float 'f', that has a precision of 2 decimal places, '.2'.



>>> countrySalesTax = 11.25
>>> purchasePrice = 124577.35
>>> print "%-18s %18.2f" % ("Country Sales Tax", countrySalesTax); \
... print "%-18s %18.2f" % ("Purchase Price", purchasePrice)
Country Sales Tax   11.25
Purchase Price  124577.35


str.format can do some things better than % interpolation, but 
formatting strings to a width is not one of them. It's also a matter of 
opinion which is more cryptic:


"%-18s %18.2f"
"{0:<18} {1:>18.2f}"



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help - want to display a number with two decimal places

2011-03-05 Thread Alan Gauld


"Modulok"  wrote

However, to make all of your numbers line up nicely, regardless of 
how

long they are, you need to look into advanced string formatting via
the builtin string method 'format()'. It takes a little practice


Or, if your version of Python doesn't support string.format() (pre 
2.6?)

you can use \t characters to insert tabs and provide a field length
value in the format string and use - signs for string alignment.
If you need more you can do double formatting where you use
an initial format string to create the output values then insert
those into the final output string.

The new format() method is more powerful in these jinds of situation
(although sadly still not as powerful as COBOLs "picture" feature 
:-( )


So your strings might look like:


   # First prints the first argument (the 0th index) to 'format(),
   # left aligned '<', and 18 characters wide, whitespace padded.
   # Then prints the second argument, (the 1st index) right aligned,
   # also 18 characters wide, but the second argument is specified 
to

   # be a float 'f', that has a precision of 2 decimal places, '.2'.


print "%-18.0s\t%18.2f" %()

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help - want to display a number with two decimal places

2011-03-04 Thread Modulok
On 3/4/11, lea-par...@bigpond.com  wrote:
> Hello
>
> I have created the following code but would like the program to include two
> decimal places in the amounts displayed to the user. How can I add this?
>
> My code:
>
>
> # Ask user to enter purchase price
> purchasePrice = input ('Enter purchase amount and then press the enter key
> $')
>
> # Tax rates
> stateTaxRate = 0.04
> countrySalesTaxRate = 0.02
>
> # Process taxes for purchase price
>
> stateSalesTax = purchasePrice * stateTaxRate
> countrySalesTax = purchasePrice * countrySalesTaxRate
>
> # Process total of taxes
> totalSalesTax = stateSalesTax + countrySalesTax
>
> # Process total sale price
> totalSalePrice = totalSalesTax + purchasePrice
>
> # Display the data
> print '%-18s %9d' % ('Purchase Price',purchasePrice)
> print '%-18s %9d' % ('State Sales Tax',astateSalesTax)
> print '%-18s %9d' % ('Country Sales Tax',countrySalesTax)
> print '%-18s %9d' % ('Total Sales tax',totalSalesTax)
> print '%-18s %9d' % ('Total Sale Price',totalSalePrice)
>

Lea,

Notice that 'd' in your string substitution means integers, not
floats. Any decimal places will be truncated when using 'd'. For
floats use 'f' instead. You an also specify a precision, such as
'%.2f' shows two decimal places.

However, to make all of your numbers line up nicely, regardless of how
long they are, you need to look into advanced string formatting via
the builtin string method 'format()'. It takes a little practice to
understand and use, but the results are exactly what you want.
(See: http://docs.python.org/library/stdtypes.html#str.format)

Here's an example you could put at the bottom of your code to see it in action:

print "{0:<18} {1:>18.2f}".format("Country Sales Tax", countrySalesTax)
print "{0:<18} {1:>18.2f}".format("Purchase Price", purchasePrice)
# First prints the first argument (the 0th index) to 'format(),
# left aligned '<', and 18 characters wide, whitespace padded.
# Then prints the second argument, (the 1st index) right aligned,
# also 18 characters wide, but the second argument is specified to
# be a float 'f', that has a precision of 2 decimal places, '.2'.


-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help - want to display a number with two decimal places

2011-03-04 Thread lea-parker
HelloI have created the following code but would like the program to include two decimal places in the amounts displayed to the user. How can I add this?My code:# Ask user to enter purchase pricepurchasePrice = input ('Enter purchase amount and then press the enter key $')
# Tax ratesstateTaxRate = 0.04countrySalesTaxRate = 0.02
# Process taxes for purchase price
stateSalesTax = purchasePrice * stateTaxRatecountrySalesTax = purchasePrice * countrySalesTaxRate
# Process total of taxestotalSalesTax = stateSalesTax + countrySalesTax
# Process total sale pricetotalSalePrice = totalSalesTax + purchasePrice
# Display the dataprint '%-18s %9d' % ('Purchase Price',purchasePrice)print '%-18s %9d' % ('State Sales Tax',astateSalesTax)print '%-18s %9d' % ('Country Sales Tax',countrySalesTax)print '%-18s %9d' % ('Total Sales tax',totalSalesTax)print '%-18s %9d' % ('Total Sale Price',totalSalePrice)Thank you in advance for your help.Lea___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor