473,231 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

String/Decimal issues


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
3 3823
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
4
by: nephish | last post by:
hey there, i have looked at the string module and re. i was looking for advice on what would be the best way to pull a value out of a small string. for example, i have a string $.+.09 JAR...
2
by: kathy | last post by:
what is the better way to determine if a string is number or not?
6
by: mahesh | last post by:
Hi friend, I am in deep trouble, I need to change the string in following format " 1.55576+2" in the floating point notation. that is i should have 155.576 as my output. Please suggest some...
2
by: yogi_bear_79 | last post by:
I have a double of unknown length that I need to split at the decimal. I thought I would convert it either to a string or a char. char seems to be the best since it easily lends itself to...
10
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I format a Number as a String with exactly 2 decimal places?...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.