Hi,
i have strings which look like money values (ie 34.45)
is there a way to convert them into float variables?
everytime i try I get this error: "numb = float(my_line) ValueError:
empty string for float()"
"
here's the code
************
import sys
import re
for line in sys.stdin.readlines():
my_line = line.rstrip()
numb = float(my_line)
print numb 16 4074
Ben Finney wrote:
You most likely do *not* want floatingpoint numbers for currency,
since they rely on the operating system's binary floating point
support which cannot accurately represent decimal fractions.
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
/W
Wildemar Wildenburger wrote:
Ben Finney wrote:
>You most likely do *not* want floatingpoint numbers for currency, since they rely on the operating system's binary floating point support which cannot accurately represent decimal fractions.
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
There are many, many situations where one is *not* trying to represent numbers
that have nice decimal fractions or the error induced is insignificant to the
problem. For example, I might take temperature readings good to 0.1 degrees
Celcius. I'll type those numbers in decimal even though when I do my
calculations I'll use floating point math because the error (about 1e15 or so)
is so far below the error in my measurement (about 0.1) that I won't have problems.
Binary floating point has the advantage of being widely implemented and quite
fast compared to decimal floating point.

Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
 Umberto Eco
Wildemar Wildenburger <la*********@klapptsowieso.netwrites:
But what use is there for floats, then? When is it OK to use them?
When one is willing to sacrifice decimal precision for speed of
calculation, and doesn't need the numbers to stay precise. E.g. when
performing millions of calculations on realworld (analogue)
measurements.

\ "Too many Indians spoil the golden egg."  Sir Joh 
`\ BjelkePetersen 
_o__) 
Ben Finney
Ben Finney wrote:
Wildemar Wildenburger <la*********@klapptsowieso.netwrites:
>But what use is there for floats, then? When is it OK to use them?
When one is willing to sacrifice decimal precision for speed of
calculation, and doesn't need the numbers to stay precise. E.g. when
performing millions of calculations on realworld (analogue)
measurements.
Traditionally, mainframe computers used BCD arithmetic to handle
currency, and the Decimal module is probably the best way to proceed if
your Python is recent enough to include it.
That isn't to say that currencies can't be handled using floatingpoint.
The important thing then becomes to round after each calculation rather
than allowing the errors to build up until they become significant
enough to make a difference to the final result.
regards
Steve

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
 Asciimercial 
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
 Thank You for Reading 
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals. There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
Floats are written as decimal fractional numbers in program code, and
they are printed as decimal fractional numbers on the screen. A novice
programmer may for this reason easily mistake floats for being
decimals. But inside the computer floats are something very different.
Floats do not represent decimal fractional numbers. Floats represent
binary fractional numbers. Decimal numbers are base10, binary numbers
are base2. Floats and decimals are similar but different.
It is much easier to manipulate for computers than decimals. Digital
electronics by convention only has two states, TTL voltage levels 0.0
V and 5.0 V, which are taken to mean 0 and 1 in binary (or false and
true in boolean) respectively. Computers work internally with binary
numbers. Floats are preferred to decimals in e.g. numerical maths and
other scientific computing for their better performance speed wise.
Knowledge of the behaviour of floats, e.g. how they cause rounding and
truncation errors, is pivotal in that field of computer science. For
most purposes, we could replace floats with decimals. But then we
would suffer a major speed penalty.
Decimals are only "precise" if the input values can be represented
precisely by decimal fractions. If we typed 1.1 and took that to mean
"one dollar and 10 cents", then certainly "exactly 1.1" was what we
meant. But if we ment "1.1 centigrades read from a mercury
thermometer", we would actually (by convention) mean "something
between 1.05 and 1.14 centigrades". Decimals also fail to be exact if
we try to compute things like "1.0/3.0". 1/3 does not have an exact
representation in decimal, and we get an approximation from the
computer.
For purposes where exact precision is not needed, decimals are neither
better nor worse than floats. This accounts for 99% of the cases where
fractional numbers are used on a computer.
More about floating point numbers here: http://www.math.byu.edu/~schow/work/...atingPoint.htm
Sturla Molden
sturlamolden wrote:
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
>I've heard (ok, read) that several times now and I understand the argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
Would you care to give an example?
There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
That last sentence is patent nonsense, and completely untrue. Many
satisfactory financial applications have been written using only
floatingpoint arithmetic. Indeed I believe the accountant's Swiss army
knife, the Excel spreadsheet, uses floatingpoint numbers exclusively.
What you say about floatingpoint have speed advantages is true, but you
go too far in claiming that decimal arithmetic is mandatory for monetary
calculations. That's about as sensible as saying that base 12 and base
20 arithmetic units were required to calculate in pounds, shillings and
pence.
regards
Steve

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
 Asciimercial 
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
 Thank You for Reading 
On 8/31/07, Steve Holden <st***@holdenweb.comwrote:
sturlamolden wrote:
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
I've heard (ok, read) that several times now and I understand the
argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
Would you care to give an example?
There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
That last sentence is patent nonsense, and completely untrue. Many
satisfactory financial applications have been written using only
floatingpoint arithmetic. Indeed I believe the accountant's Swiss army
knife, the Excel spreadsheet, uses floatingpoint numbers exclusively.
This is true, although Excel munges it's FP to provide "expected" results.
It depends on what you consider a "financial" application though.
Excel, while in extremely broad use, is not used to implement any of
the systems which actually "define" money, like the back end financial
systems at banks and credit unions.
In my experience, by far the most common method of calculating
financial numbers is actually using integer amounts, and then applying
welldefined rounding rules which I can't be bothered to look up the
name for.
For what it's worth, the work that I do with money (which is
middleware doing data transport, not calculations or billing) uses
either string representations or fixed point.
What you say about floatingpoint have speed advantages is true, but you
go too far in claiming that decimal arithmetic is mandatory for monetary
calculations. That's about as sensible as saying that base 12 and base
20 arithmetic units were required to calculate in pounds, shillings and
pence.
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use nondecimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
On Aug 31, 5:28 pm, "Chris Mellon" <arka...@gmail.comwrote:
On 8/31/07, Steve Holden <st...@holdenweb.comwrote:
sturlamolden wrote:
On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
>I've heard (ok, read) that several times now and I understand the
>argument. But what use is there for floats, then? When is it OK to use them?
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
Would you care to give an example?
There are fractions that
can be exactly represented by decimals that cannot be exactly
represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number
(e.g. 1.1) exactly. If we need that, we cannot use floats. A notable
example is monetary computations, it covers 99% of the use for decimal
numbers in computers. For this reason, we should never use floats to
add 10 cents to a dollar. The use of decimals for monetary
calculations is mandatory.
That last sentence is patent nonsense, and completely untrue. Many
satisfactory financial applications have been written using only
floatingpoint arithmetic. Indeed I believe the accountant's Swiss army
knife, the Excel spreadsheet, uses floatingpoint numbers exclusively.
This is true, although Excel munges it's FP to provide "expected" results.
It depends on what you consider a "financial" application though.
Excel, while in extremely broad use, is not used to implement any of
the systems which actually "define" money, like the back end financial
systems at banks and credit unions.
In my experience, by far the most common method of calculating
financial numbers is actually using integer amounts, and then applying
welldefined rounding rules which I can't be bothered to look up the
name for.
For what it's worth, the work that I do with money (which is
middleware doing data transport, not calculations or billing) uses
either string representations or fixed point.
What you say about floatingpoint have speed advantages is true, but you
go too far in claiming that decimal arithmetic is mandatory for monetary
calculations. That's about as sensible as saying that base 12 and base
20 arithmetic units were required to calculate in pounds, shillings and
pence.
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use nondecimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
FYI, 1 pound 4 shillings was 1.20 pounds; 1.333... pounds was 1 pound
6 shillings 8 pence. I think...
In article <ma**************************************@python.o rg>,
Chris Mellon <ar*****@gmail.comwrote:
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use nondecimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
When I worked on the British Railways National Payroll system, about 35
years ago, we, in common with many large users, wrote our system to deal
with integer amounts of pennies, and converted to pounds, shillings and
pence in the output part of the system.

David Wild using RISC OS on broadband www.davidhwild.me.uk
Chris Mellon wrote:
On 8/31/07, Steve Holden <st***@holdenweb.comwrote:
>sturlamolden wrote:
>>On 31 Aug, 02:12, Wildemar Wildenburger <lasses_w...@klapptsowieso.netwrote:
I've heard (ok, read) that several times now and I understand the argument. But what use is there for floats, then? When is it OK to use them? There are fractions that can be exactly represented by floats that cannot be exactly represented by decimals.
Would you care to give an example?
>>There are fractions that can be exactly represented by decimals that cannot be exactly represented by floats.
Which one is better? Which do we prefer?
What a float cannot do is to represent a decimal fractional number (e.g. 1.1) exactly. If we need that, we cannot use floats. A notable example is monetary computations, it covers 99% of the use for decimal numbers in computers. For this reason, we should never use floats to add 10 cents to a dollar. The use of decimals for monetary calculations is mandatory.
That last sentence is patent nonsense, and completely untrue. Many satisfactory financial applications have been written using only floatingpoint arithmetic. Indeed I believe the accountant's Swiss army knife, the Excel spreadsheet, uses floatingpoint numbers exclusively.
This is true, although Excel munges it's FP to provide "expected" results.
It depends on what you consider a "financial" application though.
Excel, while in extremely broad use, is not used to implement any of
the systems which actually "define" money, like the back end financial
systems at banks and credit unions.
In my experience, by far the most common method of calculating
financial numbers is actually using integer amounts, and then applying
welldefined rounding rules which I can't be bothered to look up the
name for.
For what it's worth, the work that I do with money (which is
middleware doing data transport, not calculations or billing) uses
either string representations or fixed point.
>What you say about floatingpoint have speed advantages is true, but you go too far in claiming that decimal arithmetic is mandatory for monetary calculations. That's about as sensible as saying that base 12 and base 20 arithmetic units were required to calculate in pounds, shillings and pence.
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use nondecimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
There is much in what you say (which, I am happy to note, doesn't really
contradict much of what I wrote). While I agree that Excel isn't a
fundamental component of backend financial systems you might be
surprised at just how many people rely on its "currency" cells for
financial applications, albeit of a more lightweight kind.
You let yourself down just a little right at the end by not knowing the
intricacies of the old sterling currency  since there were twenty
shillings to the pound and twelve pennies to a shilling that would have
been £1.2. However, you are correct in assuming that nobody tried to
represent £1 3s. 4d. as 23.333... shillings.
regards
Steve

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
 Asciimercial 
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
 Thank You for Reading 
On Aug 31, 5:39 pm, David H Wild <dhw...@talktalk.netwrote:
In article <mailman.218.1188577696.28954.pythonl...@python.org>,
Chris Mellon <arka...@gmail.comwrote:
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use nondecimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
When I worked on the British Railways National Payroll system, about 35
years ago, we, in common with many large users, wrote our system to deal
with integer amounts of pennies, and converted to pounds, shillings and
pence in the output part of the system.
So you never handled halfpennies?
MRAB wrote:
On Aug 31, 5:39 pm, David H Wild <dhw...@talktalk.netwrote:
>In article <mailman.218.1188577696.28954.pythonl...@python.org>, Chris Mellon <arka...@gmail.comwrote:
>>I believe that to the degree that "real" accounting was done in those currencies it did in fact use nondecimal bases. Just as people don't use decimal time values (except us crazy computer folk), you're write 1 pound 4 shillings, not 1.333... pounds.
When I worked on the British Railways National Payroll system, about 35 years ago, we, in common with many large users, wrote our system to deal with integer amounts of pennies, and converted to pounds, shillings and pence in the output part of the system.
So you never handled halfpennies?
Nobody was paid to the ha'penny. You could spend them in the shops, but
payroll systems didn't use them. I am happy to say that I didn't become
involved in financial programming until after decimalization (February
1971, IIRC  I was in Sweden at the time).
regards
Steve

Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
 Asciimercial 
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
 Thank You for Reading 
In article <11**********************@o80g2000hse.googlegroups .com>,
MRAB <go****@mrabarnett.plus.comwrote:
When I worked on the British Railways National Payroll system, about
35 years ago, we, in common with many large users, wrote our system to
deal with integer amounts of pennies, and converted to pounds,
shillings and pence in the output part of the system.
So you never handled halfpennies?
Halfpennies had disappeared from the BR accounting system before the first
use of computers for accounting work.

David Wild using RISC OS on broadband www.davidhwild.me.uk
On Sep 1, 4:58 am, MRAB <goo...@mrabarnett.plus.comwrote:
On Aug 31, 5:39 pm, David H Wild <dhw...@talktalk.netwrote:In article <mailman.218.1188577696.28954.pythonl...@python.org>,
Chris Mellon <arka...@gmail.comwrote:
I believe that to the degree that "real" accounting was done in those
currencies it did in fact use nondecimal bases. Just as people don't
use decimal time values (except us crazy computer folk), you're write
1 pound 4 shillings, not 1.333... pounds.
When I worked on the British Railways National Payroll system, about 35
years ago, we, in common with many large users, wrote our system to deal
with integer amounts of pennies, and converted to pounds, shillings and
pence in the output part of the system.
So you never handled halfpennies?
Or farthings?
In message <11*********************@50g2000hsm.googlegroups.c om>,
sturlamolden wrote:
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
There are no such.
On 20070901, Lawrence D'Oliveiro <ld*@geekcentral.gen.new_zealandwrote:
In message <11*********************@50g2000hsm.googlegroups.c om>,
sturlamolden wrote:
>There are fractions that can be exactly represented by floats that cannot be exactly represented by decimals.
There are no such.
In that statement does "float" mean finitelength base2 FP and
"decimal" mean finitelength base10 FP?

Grant Edwards grante Yow! My LESLIE GORE record
at is BROKEN...
visi.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jørgen Cederberg 
last post by:
Hi,
using Python 2.2.1 on Windows and QNX I'm having trouble understandig
why int() raises a ValueError exception upon converting strings.
>>> int('10')
10
>>> int('10')
10
>>> int(10.1)

by: dataangel 
last post by:
I wrote a function to compare whether two strings are "similar" because
I'm using python to make a small text adventure engine and I want to it
to be responsive to slight mispellings, like...

by: BGP 
last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow
Jones/NASDAQ/etc. stock prices as input, parse them, and do things with
it. The user can just cut and paste back prices into a...

by: Madhusudan Singh 
last post by:
Is it possible to convert a very long list of strings to a list of floats in
a single statement ?
I have tried float(x) and float(x) but neither work. I guess I would have
to write a loop if...

by: MeganTSU 
last post by:
Hey yall! I am trying to get this program finished for class.... It
says that you are suppposed to write a program that will display a
check formatted out put (the output looks like a check). I got...

by: bdog4 
last post by:
I want to validate a number to so that they can only input $2.00, 5.00,
7.00, 50.00 etc... I don't wan them to be able to put cents on the
amount. It can either either be 2 or 2.00 Any ideas on...

by: Chad 
last post by:
I would like to remove the decimal but still show the cents using
sprintf.
char empheader;
double totalwage = 123.45;
sprintf(empheader, "%.2f", totalwage);

by: SergioQ 
last post by:
Hi all, am trying to show a dollar figure on my webpage, but defining
this for the cents value:
fontsize: 80%;
verticalalign: super;
just isn't cutting it!
Is there a way to reduce the...

by: canabatz 
last post by:
i need help creating a from for input price
i want two separate fields to hold dollars and cents ,and when i press on submit
the all price will be sent as: lets say 12.45
how can i do it ,im...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...

by: Aliciasmith 
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...

by: tracyyun 
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...

by: giovanniandrean 
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...

by: NeoPa 
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currentlyselected (and open) object (Form or Report).
I know it can be done by selecting :...

by: NeoPa 
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...

by: Teri B 
last post by:
Hi, I have created a subform Roles. In my course form the user selects the roles assigned to the course.
0netomany. One course many roles.
Then I created a report based on the Course form and...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...

by: isladogs 
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
 