Skip to content

Commit 602402f

Browse files
authored
IFormattable F# snippets (#7769)
1 parent 7920fa7 commit 602402f

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// <Snippet1>
2+
open System
3+
open System.Globalization
4+
5+
type Temperature(temperature: decimal) =
6+
do
7+
if temperature < -273.15M then
8+
raise (ArgumentOutOfRangeException $"{temperature} is less than absolute zero.")
9+
10+
member _.Celsius =
11+
temperature
12+
13+
member _.Fahrenheit =
14+
temperature * 9M / 5M + 32M
15+
16+
member _.Kelvin =
17+
temperature + 273.15m
18+
19+
override this.ToString() =
20+
this.ToString("G", CultureInfo.CurrentCulture)
21+
22+
member this.ToString(format) =
23+
this.ToString(format, CultureInfo.CurrentCulture)
24+
25+
member this.ToString(format, provider: IFormatProvider) =
26+
let format =
27+
if String.IsNullOrEmpty format then "G"
28+
else format
29+
30+
let provider =
31+
if isNull provider then
32+
CultureInfo.CurrentCulture :> IFormatProvider
33+
else provider
34+
35+
match format.ToUpperInvariant() with
36+
| "G" | "C" ->
37+
temperature.ToString("F2", provider) + " °C"
38+
| "F" ->
39+
this.Fahrenheit.ToString("F2", provider) + " °F"
40+
| "K" ->
41+
this.Kelvin.ToString("F2", provider) + " K"
42+
| _ ->
43+
raise (FormatException $"The {format} format string is not supported.")
44+
45+
interface IFormattable with
46+
member this.ToString(format, provider) = this.ToString(format, provider)
47+
48+
// </Snippet1>
49+
50+
// <Snippet2>
51+
open System
52+
open System.Globalization
53+
54+
[<EntryPoint>]
55+
let main _ =
56+
// Use composite formatting with format string in the format item.
57+
let temp1 = Temperature 0
58+
Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)
59+
60+
// Use composite formatting with a format provider.
61+
let temp1 = Temperature -40
62+
String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1)
63+
|> printfn "%s"
64+
String.Format(CultureInfo "fr-FR", "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)
65+
|> printfn "%s"
66+
67+
// Call ToString method with format string.
68+
let temp1 = Temperature 32
69+
Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)\n",
70+
temp1.ToString "C", temp1.ToString "K", temp1.ToString "F")
71+
72+
// Call ToString with format string and format provider
73+
let temp1 = Temperature 100
74+
let current = NumberFormatInfo.CurrentInfo
75+
let nl = CultureInfo "nl-NL"
76+
Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
77+
temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current))
78+
Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
79+
temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl))
80+
0
81+
82+
// The example displays the following output:
83+
// 0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit)
84+
//
85+
// -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit)
86+
// -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit)
87+
//
88+
// 32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit)
89+
//
90+
// 100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit)
91+
// 100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit)
92+
// </Snippet2>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="example1.fs" />
9+
</ItemGroup>
10+
</Project>

xml/System/IFormattable.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@
8181
The following example defines a `Temperature` class that implements the <xref:System.IFormattable> 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 <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> 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 <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation.
8282
8383
:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1":::
84+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1":::
8485
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1":::
8586
8687
The following example then calls the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation either directly or by using a composite format string.
8788
8889
:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet2":::
90+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet2":::
8991
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet2":::
9092
9193
]]></format>
@@ -189,6 +191,7 @@
189191
The following example demonstrates a `Temperature` class that implements the <xref:System.IFormattable.ToString%2A> method. This code example is part of a larger example provided for the <xref:System.IFormattable> class.
190192
191193
:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1":::
194+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1":::
192195
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1":::
193196
194197
]]></format>

0 commit comments

Comments
 (0)