本文介绍了在Haskell中打印浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Haskell中有一个函数,看起来像这样
I have a function in Haskell which looks like this
type Price = Int
formatPence :: Price -> String
formatPence a = show (a `div` 100) ++ "." ++ show(a `mod` 100)
,例如,如果我输入formatPence 1023
,则输出将为"10.23"
.但是如果输入1202
会出现问题,因为输出将是"12.2"
.我应该添加什么?谢谢:)
so that, for example, if I input formatPence 1023
, the output would be "10.23"
. But I have a problem if I input 1202
because the output would be "12.2"
. What should I add? Thanks :)
推荐答案
也许您想要Numeric
中的各种show*Float
函数之一?
Perhaps you wanted one of the various show*Float
functions in Numeric
?
> :m Numeric
> showFFloat (Just 2) 123.456 ""
123.45
这篇关于在Haskell中打印浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!