float64转化成int
时间: 2023-09-28 10:13:11 浏览: 38
要将float64转换为int,可以使用类型转换操作符。在Go语言中,可以使用int()函数将float64类型转换为int类型。
例如,以下代码将一个float64类型的变量f转换为int类型:
```
f := 3.14
i := int(f)
```
在此示例中,变量f的值为3.14,而变量i的值为3。由于int()函数只保留浮点数的整数部分,因此小数部分被截断。
相关问题
Python数据分析float64转化成int
在Python数据分析中,要将float64类型转换为int类型,可以使用int()函数或astype()方法。
例如,以下代码将一个名为`f`的float64类型的Series转换为int类型:
使用int()函数:
```
import pandas as pd
f = pd.Series([3.14, 2.71, 1.618])
i = f.astype(int)
print(i)
```
输出:
```
0 3
1 2
2 1
dtype: int64
```
使用astype()方法:
```
import pandas as pd
f = pd.Series([3.14, 2.71, 1.618])
i = f.astype('int64')
print(i)
```
输出:
```
0 3
1 2
2 1
dtype: int64
```
在这两个代码示例中,我们首先创建了一个包含浮点数的Series。然后,我们使用`astype()`方法或int()函数将其转换为int类型,并将其保存在变量`i`中。在这两个示例中,输出的Series的元素类型都是int64。
numpy.float64转成int
可以使用numpy中的astype函数进行转换,代码如下:
```
import numpy as np
x = np.array([1.5, 2.7, 3.6], dtype=np.float64)
x = x.astype(int)
print(x)
```
输出结果为: [1 2 3]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)