1

How to make Haskell show Fractional values without exponent part preserving leading zeros?

For example, command show 0.0025 outputs "2.5e-3". Is there a way to redefine this behavior?

altern
  • 5,829
  • 5
  • 44
  • 72
  • You might want to try function `printf` from module [Text.Printf](https://hackage.haskell.org/package/base-4.14.0.0/docs/Text-Printf.html) ; expression `printf "%f\n" 0.0025` seems to return what you want. Of course it helps to be familiar with the printf() function in C. – jpmarinier Sep 12 '20 at 15:03

2 Answers2

4

I tested first answer in this question , and it works

import Numeric
main = do
    putStrLn (showFFloat (Just 4)  0.0025 "")

Numeric module: https://hackage.haskell.org/package/base-4.14.0.0/docs/Numeric.html

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
Howard_Roark
  • 4,088
  • 1
  • 14
  • 24
3

You can try Text.Printf.printf. The %f format should be what you want.

> import Text.Printf
> printf "%f" 0.0025  :: String
"0.0025"
chi
  • 111,837
  • 3
  • 133
  • 218