英文文档:

class float([x])

Return a floating point number constructed from a number or string x.

If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be '+' or '-'; a '+' sign has no effect on the value produced. The argument may also be a string representing a NaN (not-a-number), or a positive or negative infinity. More precisely, the input must conform to the following grammar after leading and trailing whitespace characters are removed:

sign           ::=  "+" | "-"
infinity ::= "Infinity" | "inf"
nan ::= "nan"
numeric_value ::= floatnumber | infinity | nan
numeric_string ::= [sign] numeric_value

Here floatnumber is the form of a Python floating-point literal, described in Floating point literals. Case is not significant, so, for example, “inf”, “Inf”, “INFINITY” and “iNfINity” are all acceptable spellings for positive infinity.

Otherwise, if the argument is an integer or a floating point number, a floating point number with the same value (within Python’s floating point precision) is returned. If the argument is outside the range of a Python float, an OverflowError will be raised.

For a general Python object x, float(x) delegates to x.__float__().

If no argument is given, 0.0 is returned.

说明:

  1. 函数功能将一个数值或者字符转换成浮点型数值。

>>> float(3)
3.0
>>> float('')
3.0

  2. 不提供参数的时候,返回0.0。

>>> float()
0.0

  3. 字符串必须能正确转换成浮点型数值的,否则报错。

>>> float('3.14.15926')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
float('3.14.15926')
ValueError: could not convert string to float: '3.14.15926'

  4. 字符串中允许出现“+”、“-”两个符号,两个符号和数字之间不能出现空格,但是符号前面和数字后面允许出现空格。

>>> float('+3.14') #带正号
3.14
>>> float('-3.14') #带负号
-3.14
>>> float(' -3.14 ') #正负号前、数字后可以有空格
-3.14
>>> float('- 3.14') #正负号与数字间不可以有空格
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
float('- 3.14')
ValueError: could not convert string to float: '- 3.14'

  5. 有几个特殊的字符串能正确转换,"Infinity"或者“inf”(不区分大小写),能正确转换,表示无穷大,可以和“+”、“-”一起使用;“nan”也能正确转换,表示没有值。

>>> float('Infinity')
inf
>>> float('inf')
inf >>> float('inFinIty') #不区分大小写
inf >>> float('+inFinIty') #正无穷
inf
>>> float('-inFinIty') #负无穷
-inf >>> float('nan') #没有值
nan

  6. 定义的对象如果要被float函数正确转换成浮点数,需要定义__float__函数。

>>> class X:
def __init__(self,score):
self.score = score >>> x = X(9.7)
>>> float(x) #不能转换
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
float(x)
TypeError: float() argument must be a string or a number, not 'X' >>> class X: #重新定义类,加入__float__方法
def __init__(self,score):
self.score = score
def __float__(self):
return self.score >>> x = X(9.7)
>>> float(x) #可以转换
9.7

最新文章

  1. 使用css打造形形色色的形状!
  2. mysql 最大连接数 &amp; 连接池
  3. Leetcode Copy List with Random Pointer
  4. URL重写无效
  5. nginx rewrite重写与防盗链配置
  6. php学习笔记4--php中的变量作用域
  7. HDU 2136 Largest prime factor(查找素数,筛选法)
  8. SQLServer-镜像配置
  9. jquery插件colortip(tooltip类型)
  10. Spark常见错误汇总
  11. 破解密码那些事儿(Hacking Secret Ciphers with Python)
  12. 查看oracle中的中文所占字节数
  13. java基础(六章)
  14. eclipse中JPA插件的安装与使用
  15. win10+anaconda+cuda配置dlib,使用GPU对dlib的深度学习算法进行加速(以人脸检测为例)
  16. 分布式Ruby解决之道
  17. ASP.NET Core 2.1 Web API + Identity Server 4 + Angular 6 + Angular Material 实战小项目视频
  18. 安装IDEA的历程
  19. IDEA VM设置
  20. gflags命令行参数解析

热门文章

  1. 双系统恢复CentOS的MBR
  2. C++11 带来的新特性 (2)—— 统一初始化(Uniform Initialization)
  3. 基于docker实现哨兵集群部署
  4. python 日常错误整理
  5. 如何让pandas表格直接转换为markdown表格
  6. CentOS7上安装Snipe-IT4.6.3详细过程及注意事项
  7. (lua) 基于cocos 的插入光效
  8. iphone 屏蔽系统自动更新,消除设置上的小红点
  9. C++ otlv4 连接 sql server 数据库小记
  10. jsonp 请求