如何从Double而不是Haskell中的科学计数法获取十进制字符串? [英] How to get a decimal string from a Double instead of scientific notation in Haskell?
问题描述
我需要将数字列表除以100以进行打印,例如:
I need to divide a list of numbers by 100 to be printed, for example:
map (/100) [29, 3, 12]
产生:
[0.29,3.0e-2,0.12]
但是我需要:
[0.29,0.03,0.12]
我如何在Haskell中执行此操作?任何想法真的很感激.
How do I do this in Haskell? Any ideas really appreciated.
推荐答案
0.03
和3.0e-2
是相同的数字.在内部,GHC使用 showFloat
进行打印,只要绝对值在0.1到9,999,999范围之外,就会产生科学计数法.
0.03
and 3.0e-2
are the same number. Internally, GHC uses showFloat
to print it, which will result in the scientific notation whenever the absolute value is outside the range 0.1 and 9,999,999.
因此,您必须自己打印值,例如使用printf
: showFFloat"rel =" noreferrer> showFFloat
来自Numeric
:
Therfore, you have to print the values yourself, for example with printf
from Text.Printf
or showFFloat
from Numeric
:
import Numeric
showFullPrecision :: Double -> String
showFullPrecision x = showFFloat Nothing x ""
main = putStrLn (showFullPrecision 0.03)
根据所需的输出,您需要编写更多功能.
Depending on your desired output, you need to write some more functions.
这篇关于如何从Double而不是Haskell中的科学计数法获取十进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!