[Haskell-beginners] How to convert a float or double number into a string?

yi lu zhiwudazhanjiangshi at gmail.com
Wed Sep 18 15:14:35 CEST 2013


In fact, I am not looking for some way to convert a float 0.75 to 3%4. Your
reply is helpful!
What I need is just as much number of digits as possible. If I can hold as
many digits of pi, i.e. 3.1415926535... as possible and save it in a
String, it will be perfect!

Yi


On Wed, Sep 18, 2013 at 9:00 PM, Oscar Benjamin
<oscar.j.benjamin at gmail.com>wrote:

> On 18 September 2013 13:48, yi lu <zhiwudazhanjiangshi at gmail.com> wrote:
> > If I use `show`,
> > show 123.45, it will return "123.45", a desired answer.
> >
> > However, for
> > show 123.45678901234567890, it will return
> > "123.45678901234568".
> >
> > I want to save all digits into a string. I suppose I use a wrong type of
> > number, which is Float.
>
> Yes, it is the wrong type of number. Float can only store finitely
> many digits and you're asking for slightly too many. Also even if it
> *looks like* float has enough digits for your number in fact it has
> converted them from decimal to binary. For non-integers exact decimal
> to binary conversion is rarely possible. In this case the nearest
> binary float is 123.4567890123456805895330035127699375152587890625 but
> many of these decimal digits will be truncated from display.
>
> > But what can I do to work right?
>
> Are rational numbers acceptable in your problem?
>
> http://stackoverflow.com/questions/7056791/how-to-parse-a-decimal-fraction-into-rational-in-haskell
>
>
> Oscar
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130918/ecd8c012/attachment.htm>


More information about the Beginners mailing list