String/Decimal issues

M

Mike Howarth

Hi

Seem to be having a bit of brainfreeze this evening.

Basically I'm reducing an array of prices like so:
This gives me a string of '86.00.00' which I am trying to use with decimal
objects. Python 2.4 is not particularly happy with this.

Additionally I can't simply convert the string to a decimal as it would be
invalid given it has multiple decimal points.

Being relatively new to python, I'm not sure how I could round the string or
similar. Anyone got any ideas?
 
M

Marc 'BlackJack' Rintsch

Basically I'm reducing an array of prices like so:

Take a look at the built in `sum()` function.
This gives me a string of '86.00.00' which I am trying to use with decimal
objects.

So what the heck is in `itemprices`!?
Being relatively new to python, I'm not sure how I could round the string or
similar. Anyone got any ideas?

You can't round strings. Maybe that's the problem here: data types. Are
you sure you have numbers that you are adding and not strings or something!?

Ciao,
Marc 'BlackJack' Rintsch
 
B

Ben Finney

Mike Howarth said:
Basically I'm reducing an array of prices like so:

This gives me a string of '86.00.00'

You haven't shown what the input is; what is the value of 'itemprices'
before this line?

When I use floating-point numbers, I get this::
>>> import operator
>>> itemprices = [24.68, 12.34, 36.90]
>>> itemprices [24.68, 12.34, 36.899999999999999]
>>> subtotal = reduce(operator.add, itemprices)
>>> subtotal
73.919999999999987

When I use the Decimal type, I get this::
>>> import operator
>>> from decimal import Decimal
>>> itemprices = [Decimal("24.68"), Decimal("12.34"), Decimal("36.93")] [Decimal("24.68"), Decimal("12.34"), Decimal("36.93")]
>>> subtotal = reduce(operator.add, itemprices)
>>> subtotal
Decimal("73.95")

So I suspect your 'itemprices' sequence is composed of values of some
other type. Please show your equivalent of the above sessions so we
can see what's happening.
 
S

Steven D'Aprano

Hi

Seem to be having a bit of brainfreeze this evening.

Basically I'm reducing an array of prices like so:

This gives me a string of '86.00.00' which I am trying to use with
decimal objects. Python 2.4 is not particularly happy with this.

Let me guess... your test data is:

itemprices = ['86.0', '0.00']

What happens if you use test data like this?

itemprices = ['86.0', '0.00', '1.99', '12.03']



The first problem that you have is that your prices are strings. Is that
deliberate? This is not necessarily a fault.

Your second problem is that the + operator concatenates strings, not adds
them. "1.0" + "2.0" = "1.02.0", not "3.0". This, on the other hand, is
absolutely a fault. Your code is producing invalid data.

Additionally I can't simply convert the string to a decimal as it would
be invalid given it has multiple decimal points.

Yes. Rather than trying to fix the invalid data after it is produced, you
should fix your code so it doesn't produce the wrong answer in the first
place.

Being relatively new to python, I'm not sure how I could round the
string or similar. Anyone got any ideas?


This is NOT the answer you NEED, but this is the answer you ask for. To
cut out the extra decimal places, use this:
subtotal = '86.00.00'
subtotal[:-3]
'86.00'

Now that I've told you how to do it, let me re-iterate that you shouldn't
do it. Your problem isn't that your result has an extra ".00" at the end
of the result. Your problem is that your result is WRONG. Fix the result
in the first place.

I'd be looking at something like this:


# start with a list of strings
itemprices = ['86.0', '0.00', '1.99', '12.03']
# convert them into decimal objects
itemprices = map(decimal.Decimal, itemprices)
# and add them
subtotal = sum(itemprices)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top