小编Saj*_*wer的帖子

如何在Haskell中从Float转换为Int

我在Haskell中编写了一个函数

toInt :: Float -> Int
toInt x = round $fromIntegral x
Run Code Online (Sandbox Code Playgroud)

它应该接受一个Float并返回等效的Int.我来自C编程背景,在C中,我们可以把它当作(int)x.

但是,在这种情况下,我得到以下编译错误

No instance for (Integral Float)
arising from a use of `fromIntegral'
In the second argument of `($)', namely `fromIntegral x'
In the expression: round $ fromIntegral x
In an equation for `toInt': toInt x = round $ fromIntegral x
Run Code Online (Sandbox Code Playgroud)

有关如何解决此问题的任何想法?

haskell type-conversion

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

haskell ×1

type-conversion ×1