From 3e4f75187b12193124ae759979c645e98ae5cdd5 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 25 Feb 2022 21:14:39 -0800 Subject: [PATCH] IFormattable F# snippets --- .../System/IFormattable/Overview/example1.fs | 92 +++++++++++++++++++ .../System/IFormattable/Overview/fs.fsproj | 10 ++ xml/System/IFormattable.xml | 3 + 3 files changed, 105 insertions(+) create mode 100644 snippets/fsharp/System/IFormattable/Overview/example1.fs create mode 100644 snippets/fsharp/System/IFormattable/Overview/fs.fsproj diff --git a/snippets/fsharp/System/IFormattable/Overview/example1.fs b/snippets/fsharp/System/IFormattable/Overview/example1.fs new file mode 100644 index 00000000000..3e9c4ff38bc --- /dev/null +++ b/snippets/fsharp/System/IFormattable/Overview/example1.fs @@ -0,0 +1,92 @@ +// +open System +open System.Globalization + +type Temperature(temperature: decimal) = + do + if temperature < -273.15M then + raise (ArgumentOutOfRangeException $"{temperature} is less than absolute zero.") + + member _.Celsius = + temperature + + member _.Fahrenheit = + temperature * 9M / 5M + 32M + + member _.Kelvin = + temperature + 273.15m + + override this.ToString() = + this.ToString("G", CultureInfo.CurrentCulture) + + member this.ToString(format) = + this.ToString(format, CultureInfo.CurrentCulture) + + member this.ToString(format, provider: IFormatProvider) = + let format = + if String.IsNullOrEmpty format then "G" + else format + + let provider = + if isNull provider then + CultureInfo.CurrentCulture :> IFormatProvider + else provider + + match format.ToUpperInvariant() with + | "G" | "C" -> + temperature.ToString("F2", provider) + " °C" + | "F" -> + this.Fahrenheit.ToString("F2", provider) + " °F" + | "K" -> + this.Kelvin.ToString("F2", provider) + " K" + | _ -> + raise (FormatException $"The {format} format string is not supported.") + + interface IFormattable with + member this.ToString(format, provider) = this.ToString(format, provider) + +// + +// +open System +open System.Globalization + +[] +let main _ = + // Use composite formatting with format string in the format item. + let temp1 = Temperature 0 + Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1) + + // Use composite formatting with a format provider. + let temp1 = Temperature -40 + String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1) + |> printfn "%s" + String.Format(CultureInfo "fr-FR", "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1) + |> printfn "%s" + + // Call ToString method with format string. + let temp1 = Temperature 32 + Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)\n", + temp1.ToString "C", temp1.ToString "K", temp1.ToString "F") + + // Call ToString with format string and format provider + let temp1 = Temperature 100 + let current = NumberFormatInfo.CurrentInfo + let nl = CultureInfo "nl-NL" + Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", + temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current)) + Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)", + temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl)) + 0 + +// The example displays the following output: +// 0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit) +// +// -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit) +// -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit) +// +// 32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit) +// +// 100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit) +// 100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit) +// \ No newline at end of file diff --git a/snippets/fsharp/System/IFormattable/Overview/fs.fsproj b/snippets/fsharp/System/IFormattable/Overview/fs.fsproj new file mode 100644 index 00000000000..f6fb5493c80 --- /dev/null +++ b/snippets/fsharp/System/IFormattable/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/xml/System/IFormattable.xml b/xml/System/IFormattable.xml index ab94012ea5c..bfd37763a8e 100644 --- a/xml/System/IFormattable.xml +++ b/xml/System/IFormattable.xml @@ -81,11 +81,13 @@ The following example defines a `Temperature` class that implements the interface. The class supports four format specifiers: "G" and "C", which indicate that the temperature is to be displayed in Celsius; "F", which indicates that the temperature is to be displayed in Fahrenheit; and "K", which indicates that the temperature is to be displayed in Kelvin. In addition, the implementation also can handle a format string that is `null` or empty. The other two `ToString` methods defined by the `Temperature` class simply wrap a call to the implementation. :::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1"::: The following example then calls the implementation either directly or by using a composite format string. :::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet2"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet2"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet2"::: ]]> @@ -189,6 +191,7 @@ The following example demonstrates a `Temperature` class that implements the method. This code example is part of a larger example provided for the class. :::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1"::: ]]>