Python内置函数(22)——float

时间:2022-09-02 12:27:06

英文文档:

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的更多相关文章

  1. Python内置函数&lpar;10&rpar;——float

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

  2. Python内置函数&lpar;22&rpar;——list

    英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...

  3. python 内置函数input&sol;eval(22)

    python的内置函数其实挺多的,其中input和eval算得上比较特殊,input属于交互式内置函数,eval函数能直接执行字符串表达式并返回表达式的值. 一.input函数 input是Pytho ...

  4. 学习过程中遇到的python内置函数,后续遇到会继续补充进去

    1.python内置函数isinstance(数字,数字类型),判断一个数字的数字类型(int,float,comple).是,返回True,否,返回False2.python内置函数id()可以查看 ...

  5. Python入门之 Python内置函数

    Python入门之 Python内置函数 函数就是以功能为导向,一个函数封装一个功能,那么Python将一些常用的功能(比如len)给我们封装成了一个一个的函数,供我们使用,他们不仅效率高(底层都是用 ...

  6. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  7. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

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

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

  9. Python 内置函数笔记

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

随机推荐

  1. 链接注入(便于跨站请求伪造)(AppScan扫描结果)

    最近工作要求解决下web的项目的漏洞问题,扫描漏洞是用的AppScan工具,其中此篇文章是关于链接注入问题的.下面就把这块东西分享出来. 原创文章,转载请注明 -------------------- ...

  2. WIN中SharePoint Server 2010 入门安装部署详解

    目前流行的原始安装文件基本都是这样的:Windows Server 2008 R2+SQL Server 2008R2+SharePoint Server 2010     这个初始环境原本也无可厚非 ...

  3. 《Linux内核设计与实现》读书笔记(十一)- 定时器和时间管理【转】

    转自:http://www.cnblogs.com/wang_yb/archive/2013/05/10/3070373.html 系统中有很多与时间相关的程序(比如定期执行的任务,某一时间执行的任务 ...

  4. java程序执行顺序

    原来自己一直都没弄明白Java程序的执行顺序问题,今天,自己写了个测试,果然与自己考虑的有差距 测试代码: 一个父类Animal 一个子类Dog 测试类Test 运行结果: 所以执行顺序是: 父类An ...

  5. How to distribute your own Android library through jCenter and Maven Central from Android Studio

    In Android Studio, if you wish to include any library to your application. You could just simply add ...

  6. &lbrack;Swift&rsqb;LeetCode647&period; 回文子串 &vert; Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  7. P4554 小明的游戏

    SPFA板子题 #include <stdio.h> #include <string.h> #define Clean(X,K) memset(X,K,sizeof(X)) ...

  8. js生成随机固定长度字符串的简便方法

    概述 碰到一个需求:用js生成固定长度的字符串.在网上查了很多资料,网上的方法都比较麻烦.我自己灵光一现,实现了一个比较简单的方法.记录下来,供以后开发时参考,相信对其他人也有用. js生成随机字符串 ...

  9. mysql远程连接 Host &ast; is not allowed to connect to this MySQL server(第一次配置好lnmp环境)

    1.第一次在linux上搭建好mysql,本机windows远程链接报错Host * is not allowed to connect to this MySQL server 2.原因:mysql ...

  10. git学习------>如何修改git已提交的记录中的Author和Email?

    一.背景 最近搭建好GitLab后,准备陆陆续续的将之前在SVN仓库中保存的代码迁移到GitLab上,昨天顺利将三个Android组件的代码迁移到GitLab后,其他同事发现迁移是成功了,但是pull ...