473,796 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strings (dollar.cents) into floats

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.readl ines():
my_line = line.rstrip()
numb = float(my_line)
print numb
Aug 30 '07
16 4136
Chris Mellon wrote:
On 8/31/07, Steve Holden <st***@holdenwe b.comwrote:
>sturlamolden wrote:
>>On 31 Aug, 02:12, Wildemar Wildenburger
<lasses_w...@ klapptsowieso.n etwrote:

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
calculation s is mandatory.
That last sentence is patent nonsense, and completely untrue. Many
satisfactory financial applications have been written using only
floating-point arithmetic. Indeed I believe the accountant's Swiss army
knife, the Excel spreadsheet, uses floating-point 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
well-defined 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 floating-point 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 non-decimal 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 back-end 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 -------------

Aug 31 '07 #11
On Aug 31, 5:39 pm, David H Wild <dhw...@talktal k.netwrote:
In article <mailman.218.11 88577696.28954. python-l...@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 non-decimal 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?

Aug 31 '07 #12
MRAB wrote:
On Aug 31, 5:39 pm, David H Wild <dhw...@talktal k.netwrote:
>In article <mailman.218.11 88577696.28954. python-l...@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 non-decimal 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 -------------

Aug 31 '07 #13
In article <11************ **********@o80g 2000hse.googleg roups.com>,
MRAB <go****@mrabarn ett.plus.comwro te:
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
Aug 31 '07 #14
On Sep 1, 4:58 am, MRAB <goo...@mrabarn ett.plus.comwro te:
On Aug 31, 5:39 pm, David H Wild <dhw...@talktal k.netwrote:In article <mailman.218.11 88577696.28954. python-l...@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 non-decimal 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?

Aug 31 '07 #15
In message <11************ *********@50g20 00hsm.googlegro ups.com>,
sturlamolden wrote:
There are fractions that can be exactly represented by floats that
cannot be exactly represented by decimals.
There are no such.
Sep 1 '07 #16
On 2007-09-01, Lawrence D'Oliveiro <ld*@geek-central.gen.new _zealandwrote:
In message <11************ *********@50g20 00hsm.googlegro ups.com>,
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 finite-length base-2 FP and
"decimal" mean finite-length base-10 FP?

--
Grant Edwards grante Yow! My LESLIE GORE record
at is BROKEN...
visi.com
Sep 1 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3409
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)
50
6188
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 "inevtory" and the like. To save time the first thing my function does is check if I'm comparing an empty vs. a non-empty string, because they are to be never considered similar. Right now I have to write out the check like this: if str1 and str2: if...
12
5622
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 window and hit a button to process it. The information thus enters the program as a char array. Prices can be between $1 and $100, including cents. So we can have prices such as 3.01, 1.56, 11.57, etc. The char array is an alphanumeric...
8
4887
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 there isn't a way.
7
3608
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 everything to work and it will compile and run...... BUT.... I dont know how to turn the 'Amount' into a charater string that puts the number into words.... It is supposed to show the name, then the amount, and then on the next line write out...
9
5822
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 how I can do this? Thanks
6
3121
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);
2
3619
by: SergioQ | last post by:
Hi all, am trying to show a dollar figure on my webpage, but defining this for the cents value: font-size: 80%; vertical-align: super; just isn't cutting it! Is there a way to reduce the spacing before the decimal value, so I canbring it closer to the dollar value?
3
2350
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 new to JS worls :) thanx in advance!!!
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.