python:获取没有小数位的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3398410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python: get number without decimal places
提问by l--''''''---------''''''''''''
a=123.45324
is there a function that will return just 123?
是否有一个函数只会返回123?
采纳答案by Mark Rushakoff
intwill always truncate towards zero:
int将始终向零截断:
>>> a = 123.456
>>> int(a)
123
>>> a = 0.9999
>>> int(a)
0
>>> int(-1.5)
-1
The difference between intand math.flooris that math.floorreturns the number as a float, and does not truncate towards zero.
之间的差int和math.floor是math.floor返回为浮点数的数量,并且不向零截断。
回答by Katriel
Python 2.x:
Python 2.x:
import math
int( math.floor( a ) )
N.B. Due to complicated reasons involving the handling of floats, the intcast is safe.
NB 由于涉及浮子处理的复杂原因,int演员是安全的。
Python 3.x:
Python 3.x:
import math
math.floor( a )
回答by Artur Gaspar
a = 123.45324
int(a)
回答by Avi Vajpeyi
If you want both the decimal and non-decimal part:
如果你想要小数和非小数部分:
def split_at_decimal(num):
integer, decimal = (int(i) for i in str(num).split("."))
return integer, decimal
And then:
进而:
>>> split_at_decimal(num=5.55)
(5, 55)

