How do I convert a Double to Data.Text?
In essence, I had the following code:
Data.Text.pack $ show 9.0
That code has some rather obvious silliness. So I dug around in the documentation and came up with this:
toStrict $ toLazyText $ realFloat 9.0
This seems better, but it seems like there should be a more direct method, but I can't find anything with type Double -> Data.Text. Is this the best way? It seems that if I switch to lazy Text I can avoid this, but I'm not quite ready to do that.
Any words of wisdom?
You can use the printf
like package text-format.
ClassyPrelude
provides a Show
typeclass as mentioned in Thomas' previous answer.
See tshow
and tlshow
. The latter one produces a lazy text.
Note that the default implementation is just T.fromList . Prelude.show
.
I recommend reading the Yesod blog on ClassyPrelude for general information about that package. Note that it is not a drop-in replacement for the standard prelude.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With