By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

String/Decimal issues

P: n/a

Hi

Seem to be having a bit of brainfreeze this evening.

Basically I'm reducing an array of prices like so:
>subtotal = reduce(operator.add, itemprices)
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?
--
View this message in context: http://www.nabble.com/String-Decimal...html#a13683811
Sent from the Python - python-list mailing list archive at Nabble.com.

Nov 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 10 Nov 2007 09:02:01 -0800, Mike Howarth wrote:
Basically I'm reducing an array of prices like so:
>>subtotal = reduce(operator.add, itemprices)
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
Nov 10 '07 #2

P: n/a
Mike Howarth <he*@mikehowarth.co.ukwrites:
Basically I'm reducing an array of prices like so:
subtotal = reduce(operator.add, itemprices)

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.

--
\ "Don't be afraid of missing opportunities. Behind every failure |
`\ is an opportunity somebody wishes they had missed." -- Jane |
_o__) Wagner, via Lily Tomlin |
Ben Finney
Nov 10 '07 #3

P: n/a
On Sat, 10 Nov 2007 09:02:01 -0800, Mike Howarth wrote:
Hi

Seem to be having a bit of brainfreeze this evening.

Basically I'm reducing an array of prices like so:
>>subtotal = reduce(operator.add, itemprices)

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)

--
Steven.
Nov 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.