user1063287
user1063287

Reputation: 10869

Number formatting from cents to dollars

I'm trying to use Python 2.7's string formatting to output in dollars the cost of ten apples, where the unit price is provided in cents.

I would like the value of total_apple_cost to be "10.00", but it's "1.001.001.001.001.001.001.001.001.001.00".

I have included tests for the other variables to show that they are all coming out as expected:

# define apple cost in cents
apple_cost_in_cents = 100
# define format string
cents_to_dollars_format_string = '{:,.2f}'
# convert 100 to 1.00
apple_cost_in_dollars = cents_to_dollars_format_string.format(apple_cost_in_cents / 100.)
# assign value of 'apple_cost_in_dollars' to 'apple_cost'
apple_cost = apple_cost_in_dollars
# calculate the total apple cost
total_apple_cost = 10 * apple_cost

# print out the total cost
print 'total apple cost: ' + str(total_apple_cost) + '\n'

#testing
print 'cost in cents: ' + str(apple_cost_in_cents) + '\n'
print 'cost in dollars: ' + str(apple_cost_in_dollars) + '\n'
print 'apple cost: ' + str(apple_cost) + '\n' 

solution:

thank you to answers below which both indicated that the variable 'apple_cost_in_dollars' was a string.

my solution was to make it a float and keep the rest of the code pretty much the same:

apple_cost_in_cents = 100
cents_to_dollars_format_string = '{:,.2f}'
apple_cost_in_dollars = float(cents_to_dollars_format_string.format(apple_cost_in_cents / 100.))
apple_cost = apple_cost_in_dollars
total_apple_cost = 10 * apple_cost

print 'cost in cents: ' + str(apple_cost_in_cents) + '\n'

print 'cost in dollars: $''{:,.2f}'.format(apple_cost_in_dollars) + '\n'

print 'apple cost: $''{:,.2f}'.format(apple_cost) + '\n'

print 'total apple cost: $''{:,.2f}'.format(total_apple_cost) + '\n'

Upvotes: 2

Views: 8541

Answers (4)

bentinata
bentinata

Reputation: 334

It was formatted to a string (text). So if u write 10 * string_variable, it just repeated that string 10 times. The easiest way is change this line:

total_apple_cost = 10 * apple_cost

to:

total_apple_cost = cents_to_dollars_format_string.format(10 * apple_cost_in_cents/100)

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304215

>>> import locale
>>> apple_cost_in_cents = 100
>>> locale.setlocale(locale.LC_ALL, '')
'en_US.UTF-8'
>>> locale.currency(apple_cost_in_cents * 10 / 100)
'$10.00'

Upvotes: 2

Ali-Akber Saifee
Ali-Akber Saifee

Reputation: 4596

apple_cost is a string, which you're multiplying by 10 (which simply repeats the string 10 times). Do the conversion to dollars before you format it as a string.

>>> apple_cost_in_cents = 100
>>> cents_to_dollars_format_string = '{:,.2f}'
>>> total_apple_cost_in_dollars_as_string = cents_to_dollars_format_string.format(10*apple_cost_in_cents/100.0)
>>> total_apple_cost_in_dollars_as_string
'10.00'

If you want to go further with formatting currencies you can look at the the locale module and specifically the locale.currency function.

Upvotes: 2

avasal
avasal

Reputation: 14854

it is because apple_cost_in_dollars is a string, see below

In [9]: cost = '1'

In [10]: cost * 10
Out[10]: '1111111111'

In [11]: cost = int('1')

In [12]: cost * 10
Out[12]: 10

Upvotes: 4

Related Questions