数値(Numeric)表示関数

The Haskell 98 Report 14数値 14.1 表示関数

Prelude> Numeric.showInt 123 "" -- Int を文字列に
"123"
Prelude> Numeric.showHex 123 "" -- Int を16進数文字列に
"7b"
Prelude> Numeric.showOct 123 "" -- Int を8進数文字列に
"173"
Prelude> Numeric.showIntAtBase 2 Char.intToDigit 123 "" -- 2進数文字列に
"1111011"
Prelude> Numeric.showIntAtBase 4 Char.intToDigit 123 "" -- 4進数文字列に
"1323"
Prelude> Numeric.showIntAtBase 16 Char.intToDigit 123 "" -- 16進数文字列に
"7b"
Prelude> Numeric.showIntAtBase 41 Char.intToDigit 123 "" -- 41進数文字列に
"30"
Prelude> Numeric.showIntAtBase 42 Char.intToDigit 123 ""  -- 42進数文字列に
"*** Exception: Char.intToDigit: not a digit 39

負の数は showSigned 関数を使う必要がある。

Prelude> Numeric.showInt (-123) ""
"*** Exception: Numeric.showInt: can't show negative numbers"
Prelude>  Numeric.showSigned Numeric.showInt 1 (-123) ""
"-123"

「prec はそれ を囲む文脈の優先度で・・・」 具体的には分からない・・・Orz

Prelude>  Numeric.showSigned Numeric.showInt 0 (-123) ""
"-123"
Prelude>  Numeric.showSigned Numeric.showInt 1 (-123) ""
"-123"
Prelude>  Numeric.showSigned Numeric.showInt (-1000) (-123) ""
"-123"
Prelude>  Numeric.showSigned Numeric.showInt 1000 (-123) ""
"(-123)"
Prelude>  Numeric.showSigned Numeric.showInt 1 (-123) ""
"-123"
Prelude>  Numeric.showSigned Numeric.showInt 10 (-123) ""
"(-123)"
Prelude>  Numeric.showSigned Numeric.showInt 5 (-123) ""
"-123"

(showEFloat digs val) の呼び出しにおいて、 digs が Nothing なら、その値は丸めなしで表示され、 digs が Just d ならば、小数点以下は 多くても d 桁で表示される。

Prelude>  Numeric.showFFloat Nothing 0.123456789 ""
"0.123456789"
Prelude>  Numeric.showFFloat (Just 4) 0.123456789 ""
"0.1235"
Prelude>  Numeric.showFFloat (Just 20) 0.123456789 ""
"0.12345678900000000000"
 
Prelude>  Numeric.showEFloat Nothing 0.123456789 ""
"1.23456789e-1"
Prelude>  Numeric.showEFloat (Just 4) 0.123456789 ""
"1.2346e-1"