How to convert a float or double number into a string?

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. But what can I do to work right? Yi PS: If you receive this mail twice, please ignore one of them.

On 18 September 2013 13:48, yi lu
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-i... Oscar

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
On 18 September 2013 13:48, yi lu
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-i...
Oscar _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sep 18, 2013 2:15 PM, "yi lu"
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

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 wrote: On Sep 18, 2013 2:15 PM, "yi lu" 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@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

Thishttps://github.com/eccstartup/numberToText/blob/master/src/Text/New/NumberTo...is
my poor code.
*show* function truncated these digits.
On Wed, Sep 18, 2013 at 11:07 PM, yi lu
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@gmail.com> wrote:
On Sep 18, 2013 2:15 PM, "yi lu"
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
Thishttps://github.com/eccstartup/numberToText/blob/master/src/Text/New/NumberTo...is my poor code. *show* function truncated these digits.
On Wed, Sep 18, 2013 at 11:07 PM, yi lu
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@gmail.com> wrote:
On Sep 18, 2013 2:15 PM, "yi lu"
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

It you don't mind truncation from the outset there is Data.Fixed.
On 18 September 2013 17:16, David McBride
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.

On 18 September 2013 16:07, yi lu
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)".
You should probably explain this at the beginning!
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!
I suspect you don't yet understand what a float really is: http://en.wikipedia.org/wiki/Single-precision_floating-point_format If you do understand what a single (or double) precision float is then consider this: most values cannot be exactly represented by the float type. When you ask Haskell to create a float with some value given as a decimal string it will usually not return a float with the exact value you asked for since this is often impossible. Instead it will only promise to give you a "close" value. For your problem I would just use string manipulation. Testing for n<1000000 is just as easy as testing the length of a decimal string. Conversion to float or Rational is usually only worthwhile if you're intending to do some arithmetic which you're not. So your input is a string your output should be a string and you're not doing arithmetic: you may as well view this as a text processing problem and just write functions that work with strings. Oscar

On Wed, Sep 18, 2013 at 8:14 PM, yi lu
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!
Perhaps you had some experience with a computer algebra system (Maple, Mathematica, etc.) and was expecting similar behavior? While it's certainly possible to build it on top of Haskell, these kind of floating-point features aren't available out of the box. Haskell provides only fixed-bitwidth floating point. As Oscar recommended, you probably want a String -> String function, say
toEnglish "1.239402874" "one point two three nine four zero blah blah"
-- Kim-Ee

Thank you all! I am considering defining a String -> String function now.
On Fri, Sep 20, 2013 at 12:39 AM, Kim-Ee Yeoh
On Wed, Sep 18, 2013 at 8:14 PM, yi lu
wrote: 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!
Perhaps you had some experience with a computer algebra system (Maple, Mathematica, etc.) and was expecting similar behavior?
While it's certainly possible to build it on top of Haskell, these kind of floating-point features aren't available out of the box. Haskell provides only fixed-bitwidth floating point.
As Oscar recommended, you probably want a String -> String function, say
toEnglish "1.239402874" "one point two three nine four zero blah blah"
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
David McBride
-
Kim-Ee Yeoh
-
Oscar Benjamin
-
Stephen Tetley
-
yi lu