Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the least verbose way to convert a Double to Data.Text?

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?

like image 459
Tim Perry Avatar asked Jun 23 '11 03:06

Tim Perry


2 Answers

You can use the printf like package text-format.

like image 146
tibbe Avatar answered Oct 01 '22 08:10

tibbe


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.

like image 43
Uli Köhler Avatar answered Oct 01 '22 08:10

Uli Köhler