[Haskell-beginners] How to convert a float or double number into a string?
David McBride
toad3k at gmail.com
Wed Sep 18 18:16:24 CEST 2013
Show is not truncating anything. Show never had those digits to read from
in the first place. The problem is that once you convert to floats or
doubles, the number will be truncated. They do not have infinite
precision. You cannot pass a literal float and have it go to infinite
precision. You have two options:
You either pass the float as a string ie toText "1.23456"
Or you use rationals toText (
3.141591234567123123897192712937123987123987123 :: Rational) and do the
arithmetic to change that into text
3141591234567123123897192712937123987123987123 %
1000000000000000000000000000000000000000000000
import Numeric
import Data.Rational
import GHC.Real
let (num :% den) = fst $ head $ (readSigned readFloat)
"3.141591234567123123897192712937123987123987123" :: Rational
num -> 3141591234567123123897192712937123987123987123
den -> 1000000000000000000000000000000000000000000000
Then use the size of the denominator to figure out how far over to move the
"point" in the first number.
On Wed, Sep 18, 2013 at 11:12 AM, yi lu <zhiwudazhanjiangshi at gmail.com>wrote:
> This<https://github.com/eccstartup/numberToText/blob/master/src/Text/New/NumberToText.hs#L104>is my poor code.
> *show* function truncated these digits.
>
>
> On Wed, Sep 18, 2013 at 11:07 PM, yi lu <zhiwudazhanjiangshi at gmail.com>wrote:
>
>> You are right! I hope to input a number, for example 123, and output its
>> text "one hundred and twenty-three".
>> So, for 1.23456789012345678901, I want the result is "one point two three
>> four five six ...(something omitted)".
>> I can define a funciton, say "toText", to preform this action.
>> In ghci, I can use like this.
>> Prelude>toText 123.45
>> "one hundred and twenty-three point four five"
>>
>> However, in this function, I have to read this number as
>> String(originally a number, now "123"), and make it to words(String) like
>> "one two three".
>> But for a float number, it will not work very well.
>> Prelude>toText 1.23456789012345678901
>> "(a truncated answer)"
>>
>> This confuses me!
>>
>> Yi
>>
>>
>> On Wed, Sep 18, 2013 at 10:23 PM, Oscar Benjamin <
>> oscar.j.benjamin at gmail.com> wrote:
>>
>>>
>>> On Sep 18, 2013 2:15 PM, "yi lu" <zhiwudazhanjiangshi at gmail.com> wrote:
>>> >
>>> > 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!
>>>
>>> I think something is still missing from your description.
>>>
>>> If you want to store the digits of pi in a string then why not use a
>>> string?
>>>
>>> Oscar
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>
>
> _______________________________________________
> 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/6c62f34c/attachment.htm>
More information about the Beginners
mailing list