473,382 Members | 1,290 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,382 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 3831
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?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.