英文文档:

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 OverflowErrorwill be raised.

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

If no argument is given, 0.0 is returned.

说明:

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

>>> float(3)
3.0
>>> float('3')
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

Python内置函数(10)——float的更多相关文章

  1. Python内置函数(22)——float

    英文文档: class float([x]) Return a floating point number constructed from a number or string x. If the ...

  2. Python内置函数(10)——chr

    英文文档: chr(i) Return the string representing a character whose Unicode code point is the integer i. F ...

  3. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  4. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  5. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  6. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  7. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  8. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  9. Python内置函数5

    Python内置函数5 1.format参考前面字符串方法中的format 2.frozenset([iterable]) iterable -- 可迭代的对象,比如列表.字典.元组等等 返回一个冻结 ...

随机推荐

  1. 图之单源Dijkstra算法、带负权值最短路径算法

    1.图类基本组成 存储在邻接表中的基本项 /** * Represents an edge in the graph * */ class Edge implements Comparable< ...

  2. 【Unity3D与23种设计模式】享元模式(Flyweight)

    GoF中定义: "使用共享的方式,让一大群小规模对象能更有效地运行" 享元模式一般应用在游戏角色属性设置上 游戏策划需要通过"公式计算"或者"实际测试 ...

  3. 设计模式——享元模式(C++实现)

    #include <iostream> #include <string> #include <map> #include <vector> #incl ...

  4. mySQL语法中的存储过程及if语句的使用简例

    create procedure gh() #注意各个地方的分号!此代码应先运行除掉最后一句的部分,然后运行call gh显示已经存储的结果 BEGIN declare c_no int; #声明数据 ...

  5. python趣味 ——奇葩的全局形参

    在c++,c#,js等语言中: 函数定义(参数) 函数体:参数修改 这里的参数修改都是仅限于这个函数体内的 python不知道是不是bug,我们这样写: def test(a=[]): a.appen ...

  6. svn打分支

    http://www.07net01.com/linux/Eclipsexiasvndechuangjianfenzhi_hebing_qiehuanshiyong_548928_1374750252 ...

  7. 1.使用dom4j解析XML文件

    一.dom4j的简介 dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,它 ...

  8. Android Service 基础

    启动方式 startService(Intent) 这种方式启动的Service可以在后台无限期的运行,与启动它的组件没有关系. bindService 绑定Service.它提供了一种类似C/S结构 ...

  9. 【Python】excel读写操作 xlrd & xlwt

    xlrd ■ xlrd xlrd模块用于读取excel文件内容 基本用法: workbook = xlrd.open_workbook('文件路径') workbook.sheet_names() # ...

  10. Algorithm --> 阶乘和因子

    阶乘和因子 要求:输入正整数n(1<n <101), 把阶乘n!=1x2x3x...xn分解成素因子相乘的形式,从小到大输出各个素数(2,3,5,...)的指数. 例如825=3x52x1 ...