convert a float to string in F#?
Solution 1
> sprintf "%f";;
val it : (float -> string) = <fun:it@8>
Solution 2
As others pointed out, there are a few options. The two simplest are calling ToString
method and using string
function. There is a subtle difference between the two that you should be aware of. Here is what they do on my system:
> sprintf "%f" 1.2;;
val it : string = "1.200000"
> string 1.2;;
val it : string = "1.2"
> 1.2.ToString();;
val it : string = "1,2"
The first two are different, but both make sense, but why the heck did the last one return "1,2"?
That's because I have Czech regional settings where decimal point is written as comma (doh!) So, the string
function uses invariant culture while ToString
uses current culture (of a thread). In some weird cultures (like Czech :-)) this can cause troubles! You can also specify this explicitly with the ToString
method:
> 1.2.ToString(System.Globalization.CultureInfo.InvariantCulture);;
val it : string = "1.2"
So, the choice of the method will probably depend on how you want to use the string - for presentation, you should respect the OS setting, but for generating portable files, you probably want invariant culture.
Solution 3
Use the 'string' function.
string 6.3f
Solution 4
string;;
val it : (obj -> string) = <fun:it@1>
Solution 5
Just to round out the answers:
(fun (x:float) -> x.ToString())
:)
Related videos on Youtube
Ramy
Updated on January 29, 2020Comments
-
Ramy about 4 years
How do I convert a float to a string in F#. I'm looking for a function with this signature:
float -> string
-
Tomas PetricekIt really depends on how you want the resulting string to look. It may be more subtle than you think :-)
-
-
gradbot about 13 yearsThis was fun answering just to see how fast others would post and up vote.
-
Boti about 13 yearsMight as well add in
System.Convert.ToString
if you're trying to round out the answers ;) -
elmattic about 13 yearsAdditionally you have an equivalent conversion function for other basic types with functions
int
,uint32
,float32
,byte
,char
,decimal
, etc. -
Stephen Swensen about 13 yearsThe signature is deceptive here, since
string
is an inline statically optimized function and, when used with float, translates into something likelet x = 3.2 in (# "" x : float #).ToString("g",System.Globalization.CultureInfo.InvariantCulture)
. Constraining the signature like(string:float->string)
is a better illustration I think. -
gradbot about 13 yearsI agree. MSDN also list it as the casting operator. msdn.microsoft.com/en-us/library/dd233220.aspx I'd imagine most people would want to use sprintf "%f" so you can format the float how you want.
-
Tomas Petricek about 13 years@gradbot: It was answered fast, but all answers ignored subtle aspects of fun cultures :-)!
-
elmattic about 13 yearsGood point, I have "1,2" too :). Fun fact: the weird cultures almost stalled developement of ALGOL according to wikipedia.
-
Ramy about 13 yearscouldn't select the answer for 11 minutes for some reason. So...I forgot about it for about a day.
-
Friedrich Gretz almost 7 yearsThere is another difference between the
string
function and theToString
method. Try applying them tonull
andNone
. Whilestring
always returns an empty string, theToString
method crashes.