convert a float to string in F#?

10,735

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())

:)

Share:
10,735

Related videos on Youtube

Ramy
Author by

Ramy

Updated on January 29, 2020

Comments

  • Ramy
    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 Petricek
      Tomas Petricek
      It really depends on how you want the resulting string to look. It may be more subtle than you think :-)
  • gradbot
    gradbot about 13 years
    This was fun answering just to see how fast others would post and up vote.
  • Boti
    Boti about 13 years
    Might as well add in System.Convert.ToString if you're trying to round out the answers ;)
  • elmattic
    elmattic about 13 years
    Additionally you have an equivalent conversion function for other basic types with functions int, uint32, float32, byte, char, decimal, etc.
  • Stephen Swensen
    Stephen Swensen about 13 years
    The signature is deceptive here, since string is an inline statically optimized function and, when used with float, translates into something like let x = 3.2 in (# "" x : float #).ToString("g",System.Globalization.CultureInfo.InvariantCu‌​lture). Constraining the signature like (string:float->string) is a better illustration I think.
  • gradbot
    gradbot about 13 years
    I 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
    Tomas Petricek about 13 years
    @gradbot: It was answered fast, but all answers ignored subtle aspects of fun cultures :-)!
  • elmattic
    elmattic about 13 years
    Good point, I have "1,2" too :). Fun fact: the weird cultures almost stalled developement of ALGOL according to wikipedia.
  • Ramy
    Ramy about 13 years
    couldn't select the answer for 11 minutes for some reason. So...I forgot about it for about a day.
  • Friedrich Gretz
    Friedrich Gretz almost 7 years
    There is another difference between the string function and the ToString method. Try applying them to null and None. While string always returns an empty string, the ToString method crashes.

Related