在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