简体   繁体   English

如何在python中将浮点数转换为整数?

[英]How can I convert a floating point number to an integer in python?

I have given an input of 3 arguments to a script from powershell and I want one of these to be the floating point number 0.0. 我从powershell给脚本输入了3个参数,我希望其中之一是0.0的浮点数。 I then try to convert this value from a string to an integer using time = int(time) but I get an error. 然后,我尝试使用time = int(time)将此值从字符串转换为整数,但出现错误。 Anyone know how I can get my variable inside the program to hold my float point number? 有人知道如何在程序中获取变量以保存浮点数吗? Thank you. 谢谢。

time = int(float(time))

Are you sure you want an integer here? 您确定要在这里整数吗? It doesn't make sense to pass in "0.0" if you want integer 0 . 如果要整数0则传递"0.0"没有意义。 It sounds like either you should be passing in "0" , or you should be storing it as a float. 听起来您应该传递"0"或将其存储为浮点数。

To store it as a float, just use 要将其存储为浮点数,只需使用

time = float(time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中获取浮点数的前n位作为整数 - How can I get first n bits of floating point number as integer in python 如何在 Python 中将货币字符串转换为浮点数? - How do I convert a currency string to a floating point number in Python? 如何在python中转换浮点数? - How to convert floating point number in python? 如何在python中将浮点数转换为基数3 - How to convert floating point number to base 3 in python 我如何计算字符串中的浮点数,将浮点数识别为一个数字? -Python - How can I count number of floating points in a string, recognizing the floating point as one number? - Python 如何在python中使用整数运算将分数转换为浮点数? - How do I use integer arithmetic to convert fractions into floating point numbers in python? 如何将 Sympy 表达式转换为浮点数? - How do I convert a Sympy expression to a floating point number? 如何在 python 中将外部文件中的数字转换为 integer - How can I convert a number in an external file into a integer , in python 在Python中:如何使用指定数量的数字在文件中编写浮点数? - In Python: How can I write floating point numbers in a file using a specified number of figures? 如何设置浮点精度以避免Python中的浮点运算错误? - How can I set floating point precision to avoid floating point arithmetic error in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM