Python内置函数(22)——float
英文文档:
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
Python内置函数(22)——float的更多相关文章
- Python内置函数(10)——float
英文文档: class float([x]) Return a floating point number constructed from a number or string x. If the ...
- Python内置函数(22)——list
英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...
- python 内置函数input/eval(22)
python的内置函数其实挺多的,其中input和eval算得上比较特殊,input属于交互式内置函数,eval函数能直接执行字符串表达式并返回表达式的值. 一.input函数 input是Pytho ...
- 学习过程中遇到的python内置函数,后续遇到会继续补充进去
1.python内置函数isinstance(数字,数字类型),判断一个数字的数字类型(int,float,comple).是,返回True,否,返回False2.python内置函数id()可以查看 ...
- Python入门之 Python内置函数
Python入门之 Python内置函数 函数就是以功能为导向,一个函数封装一个功能,那么Python将一些常用的功能(比如len)给我们封装成了一个一个的函数,供我们使用,他们不仅效率高(底层都是用 ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
随机推荐
- 微信域名检测的C#实现
背景:最近公司的公众号域名被封了,原因是公司网站被黑后上传了一个不符合微信规范的网页.所以...就进入了微信域名解封的流程. 百度微信域名解封发现很多微信域名检测的网站,还有Api:但是本人做微信公 ...
- gitlab搭建,结合pycharm和vs2015配置用于开发python和c++
利用Ubuntu16.04服务器搭建gitlab仓库,本地windows系统使用pycharm和VS开发,通过软件配置可进行代码管理. 1.gitlab安装 ①安装依赖包: sudo apt-get ...
- UOJ#375. 【ZJOI2018】迷宫
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ375.html 题解 首先,我们可以建出一个 k 个点的自动机,第 i 个点表示当前数对 k 取模为 i- ...
- net core EF 链接mysql 数据库
这个主要是一个demo.就在一个工程里面写的 安装MySql.Data.EntityFrameworkCore 增加DbContext 相当于程序与数据库的中间层 public class Ident ...
- Docker使用问题记录贴
请参考: https://blog.csdn.net/u013948858/article/details/78429954 问题:安装Docker之后,执行docker run hello-worl ...
- hadoop hdfs ha 模式
这是我自己在公司一个搭建公司大数据框架是自己的选项,在配置yarn ha 出现了nodemanager起不来的问题于是我把yarn搭建为普通yarn 如果有人解决 高yarn的nodemanager问 ...
- HTTP协议头部与Keep-Alive模式详解(转)
转自:http://a280606790.iteye.com/blog/1095085 http1.1 中怎么打开持久连接,怎么关闭,怎么传输数据(确定本次数据是否传输完毕) 1.什么是Keep-Al ...
- [POJ1193][NOI1999]内存分配(链表+模拟)
题意 时 刻 T 内存占用情况 进程事件 0 1 2 3 4 5 6 7 8 9 进程A申请空间(M=3, P=10)<成功> 1 A 2 A B 进程B申请空间(M=4, P=3)< ...
- phpstorm 断点调试 傻瓜教程
前言: 简单介绍下为什么要用断点调试,很多人说我在代码调试的部位用var_dump 或者 exit 或者print_r来进行断点,但是当项目足够大的时候这样的做法就比较费时费力,因为你断点后需要删除原 ...
- Burp Suite Pro 教程
1.Burp Suite Pro2.0.11破解版-2018.11.06更新 说明基地址 来源:http://ximcx.cn/post-110.html 启动;如果是用的burp2.0,把下面的代码 ...