英文文档:

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 = scoredef __float__(self):return self.score>>> x = X(9.7)
>>> float(x) #可以转换
9.7

转载于:https://www.cnblogs.com/lincappu/p/8144674.html

Python内置函数(10)——float相关推荐

  1. python内置函数之float() 函数

    文章目录 float() 函数 描述 语法 参数 返回值 实例 float() 函数 描述 float() 函数用于将整数和字符串转换成浮点数. 语法 float()方法语法: class float ...

  2. python中chr函数的用法_【转】Python内置函数(10)——chr

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

  3. 介绍10个常用的Python内置函数,99.99%的人都在用!

    人生苦短,快学Python! 对于Python内置函数,在心里想一下:什么是Python内置函数呢? 内置函数简介 Python 解释器自带的函数叫做"内置函数",这些函数不需要i ...

  4. python内置函数多少个_每个数据科学家都应该知道的10个Python内置函数

    python内置函数多少个 Python is the number one choice of programming language for many data scientists and a ...

  5. 【Python】Python3.7.3 - Python内置函数

    文章目录 系统参数 Python内置函数 abs() all() any() ascii() repr() eval() 空值为假,非空为真 系统参数 [tony@tony-controller bi ...

  6. python内置函数返回元素个数_Python内置函数

    Python Python开发 Python语言 Python内置函数 Python内置函数 一.内置函数 什么是内置函数? 就是python给你提供的. 拿来直接⽤的函数, 比如print, inp ...

  7. Python内置函数、匿名函数

    内置函数 我们一起来看看python里的内置函数.什么是内置函数?就是Python给你提供的,拿来直接用的函数,比如print,input等等.截止到python版本3.6.2,现在python一共为 ...

  8. Python内置函数—vars的具体使用方法

    本文文章主要介绍了Python内置函数-vars的具体使用方法,分享给大家,具体如下: 英文文档: vars([object]) Return the dict attribute for a mod ...

  9. 大学计算机python编程的函数及作用总结_【转】python 内置函数总结(大部分)...

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

  10. python内置函数用来返回序列中的最小元素_1000道Python题库系列分享二(48道)

    1.10 pip list 1.11 对 1.12 错 1.13 错 1.14 错 1.15 对 1.16 错 1.17 对 本期题目: 2.1 表达式int('11111', 2)的值为______ ...

最新文章

  1. Yii2 使用 Joins 查询
  2. Javascript 强制类型转换函数
  3. Python+selenium 自动化-selenium的版本查看和升级
  4. C语言fstat函数获取文件的大小
  5. 优化SQL查询:如何写出高性能SQL语句
  6. init.d文件夹 2012-02-09
  7. 数据可视化(一) 线条曲线
  8. 第六章 输入输出系统-作业
  9. 宿主程序Crash与Lua是动态库还是静态库有关?
  10. 生成手写文字图片_如何把手机图片转成PDF文件?这个技巧你一定能学会!
  11. DSP28335 CAN通讯实验
  12. QT 控件加载图片不显示
  13. python处理图片去白底-Python实现将蓝底照片转化为白底照片功能完整实例
  14. 【c/c++编程】数学类问题:同余模、最大公约数、最小公倍数、素数判定
  15. restful 验证码平台请求验证
  16. CNN Tensorflow 入门——以Cifar-10为例
  17. linux 字体 命令,Linux下的字体
  18. 【前端】js 视频笔记(DOM BOM)
  19. 【VBA研究】VBA编程产生不重复随机数
  20. java计算机毕业设计体育竞赛成绩管理系统源码+数据库+系统+lw文档+mybatis+运行部署

热门文章

  1. DailyWallpaper v1.02 released
  2. 招聘 | 刘知远老师博士生涂存超博士创办的「幂律智能」招NLP算法工程师啦
  3. 抖音日活用户破 6 亿,推荐系统是怎么做到的?
  4. 这些进程的后台可靠运行命令你都知道了吗
  5. 2020国际机器翻译大赛:火山翻译力夺五项冠军
  6. 深度学习2.0-28.其他训练tricks-Early Stopping,dropout等
  7. 各路技术牛人都推荐的书
  8. 博文视点Open Party第5期:操作系统与嵌入式开发 圆满结束
  9. 要做网络营销 这样的讲座不能错过
  10. 二十一天学通C语言:使用const声明指针变量