字符串(dollar.cents)成浮点数 [英] strings (dollar.cents) into floats

查看:59
本文介绍了字符串(dollar.cents)成浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i有字符串看起来像货币值(即34.45)

有没有办法将它们转换为浮点变量?

每次尝试我都会收到此错误:" numb = float(my_line)ValueError:

float()的空字符串"

"

这里'是代码


************


import sys



for sys.stdin.readlines()中的行:

my_line = line.rstrip()

numb = float(my_line)

打印麻木

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

推荐答案

Ben Finney写道:
Ben Finney wrote:

你很可能*不*想要货币的浮点数,

,因为它们依赖于操作系统的二进制浮点数

支持无法准确表示小数部分。
You most likely do *not* want floating-point numbers for currency,
since they rely on the operating system''s binary floating point
support which cannot accurately represent decimal fractions.



我已经听过(好了,读了)好几次,我明白了

论点。但那么花车有什么用呢?什么时候可以使用它们?


/ W

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写道:
Wildemar Wildenburger wrote:

Ben Finney写道:
Ben Finney wrote:

>你很可能*不*想要货币的浮点数,
因为他们依赖于操作系统的二进制浮点支持,它无法准确表示小数部分。
>You most likely do *not* want floating-point 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?



有很多种情况,其中一个*不*试图表示数字

有很好的小数部分或引起的错误是

问题无关紧要。例如,我可能会将温度读数调好到0.1度

Celcius。我会输入十进制的数字,即使我做了我的

计算我会使用浮点数学,因为错误(约1e-15左右)

远远低于我的测量误差(大约0.1),我不会遇到问题。


二进制浮点具有广泛实现的优点,而且非常好>
快速与十进制浮点数相比。


-

Robert Kern


"我开始相信整个世界都是一个谜,一个无害的谜团,因为我们疯狂地试图解释它,好像它有一个潜在的基础,这让我觉得很可怕。真相。

- Umberto Eco

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 1e-15 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:
Wildemar Wildenburger <la*********@klapptsowieso.netwrites:

但是那里有什么用于花车呢?什么时候可以使用它们?
But what use is there for floats, then? When is it OK to use them?



当一个人愿意牺牲小数精度来计算

的速度时,并且不需要数字保持精确。例如。当

在真实世界(模拟)上进行数百万计算

测量时。


-

\太多的印第安人破坏了金蛋。 - 约翰爵士|

` \ Bjelke-Petersen |

_o__)|

Ben Finney

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 real-world (analogue)
measurements.

--
\ "Too many Indians spoil the golden egg." -- Sir Joh |
`\ Bjelke-Petersen |
_o__) |
Ben Finney


这篇关于字符串(dollar.cents)成浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆