In JS we have Number.toExponential()
which converts converts a number into its scientific notation (i.e : for 1000 -> "1e3"
).
I checked on hoggle but I can't seem to find it.
Note : toExponential changes num -> string
CodePudding user response:
You can make use of the printf :: PrintfType r => String -> r
and work with a %e
or %E
specifier:
Prelude> import Text.Printf
Prelude Text.Printf> printf "%e" 14.25 :: String
"1.425e1"
Prelude Text.Printf> printf "%E" 14.25 :: String
"1.425E1"
here %e
specifies the scientific notation with a lowercase e
, and the %E
with an uppercase E
. The output type of printf
can be a String
, or an IO ()
. If the String
type is used we get a String
with the formatted type, for IO ()
it will print the type to the stdout.
@Noughtmare also mentioned the showEFloat :: RealFloat a => Maybe Int -> a -> String -> String
to prepend a string with the exponential representation of a RealFloat
number type.
You can also work with a Scientific
number, and then work with formatScientific :: FPFormat -> Maybe Int -> Scientific -> String
.
If you thus install the scientific
package, we can implement this with:
Prelude> import Data.Scientific
Prelude Data.Scientific> formatScientific Exponent Nothing 1000
"1.0e3"
This thus means that the 1000
type should be Scientific
not a `