Skip to content

Commit c66fa89

Browse files
authored
IFormatProvider F# snippets (#7768)
1 parent 6131a03 commit c66fa89

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
module Provider
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
type AcctNumberFormat() =
8+
let [<Literal>] ACCT_LENGTH = 12
9+
10+
interface IFormatProvider with
11+
member this.GetFormat(formatType: Type) =
12+
if formatType = typeof<ICustomFormatter> then
13+
this
14+
else
15+
null
16+
17+
interface ICustomFormatter with
18+
member this.Format(fmt: string, arg: obj, formatProvider: IFormatProvider) =
19+
// Provide default formatting if arg is not an Int64.
20+
// Provide default formatting for unsupported format strings.
21+
let ufmt = fmt.ToUpper CultureInfo.InvariantCulture
22+
if arg.GetType() = typeof<Int64> && (ufmt = "H" || ufmt = "I") then
23+
// Convert argument to a string.
24+
let result = string arg
25+
26+
let result =
27+
// If account number is less than 12 characters, pad with leading zeroes.
28+
if result.Length < ACCT_LENGTH then
29+
result.PadLeft(ACCT_LENGTH, '0')
30+
else result
31+
32+
let result =
33+
// If account number is more than 12 characters, truncate to 12 characters.
34+
if result.Length > ACCT_LENGTH then
35+
result.Substring(0, ACCT_LENGTH)
36+
else result
37+
38+
// Integer-only format.
39+
if ufmt = "I" then
40+
result
41+
// Add hyphens for H format specifier.
42+
else // Hyphenated format.
43+
result.Substring(0, 5) + "-" + result.Substring(5, 3) + "-" + result.Substring(8)
44+
else
45+
try
46+
this.HandleOtherFormats(fmt, arg)
47+
with :? FormatException as e ->
48+
raise (FormatException($"The format of '{fmt}' is invalid.", e))
49+
50+
member _.HandleOtherFormats(format: string, arg: obj) =
51+
match arg with
52+
| :? IFormattable as arg ->
53+
arg.ToString(format, CultureInfo.CurrentCulture)
54+
| null ->
55+
string arg
56+
| _ ->
57+
String.Empty
58+
// </Snippet2>
59+
60+
61+
// <Snippet1>
62+
open System
63+
open System.Globalization
64+
65+
type DaysOfWeek = Monday = 1 | Tuesday = 2
66+
67+
[<EntryPoint>]
68+
let main _ =
69+
let acctNumber = 104254567890L
70+
let balance = 16.34
71+
let wday = DaysOfWeek.Monday
72+
73+
let output =
74+
String.Format(AcctNumberFormat(),
75+
"On {2}, the balance of account {0:H} was {1:C2}.",
76+
acctNumber, balance, wday)
77+
printfn $"{output}"
78+
79+
let wday = DaysOfWeek.Tuesday
80+
let output =
81+
String.Format(AcctNumberFormat(),
82+
"On {2}, the balance of account {0:I} was {1:C2}.",
83+
acctNumber, balance, wday)
84+
printfn $"{output}"
85+
0
86+
87+
// The example displays the following output:
88+
// On Monday, the balance of account 10425-456-7890 was $16.34.
89+
// On Tuesday, the balance of account 104254567890 was $16.34.
90+
// </Snippet1>
91+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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="provider2.fs" />
9+
<Compile Include="Provider.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module provider2
2+
3+
// <Snippet3>
4+
open System
5+
open System.Globalization
6+
7+
let dateValue = DateTime(2009, 6, 1, 16, 37, 0)
8+
let cultures =
9+
[ CultureInfo "en-US"
10+
CultureInfo "fr-FR"
11+
CultureInfo "it-IT"
12+
CultureInfo "de-DE" ]
13+
14+
for culture in cultures do
15+
printfn $"{culture.Name}: {dateValue.ToString culture}"
16+
// The example displays the following output:
17+
// en-US: 6/1/2009 4:37:00 PM
18+
// fr-FR: 01/06/2009 16:37:00
19+
// it-IT: 01/06/2009 16.37.00
20+
// de-DE: 01.06.2009 16:37:00
21+
// </Snippet3>

xml/System/IFormatProvider.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,19 @@
6969
The following example illustrates how an <xref:System.IFormatProvider> implementation can change the representation of a date and time value. In this case, a single date is displayed by using <xref:System.Globalization.CultureInfo> objects that represent four different cultures.
7070
7171
:::code language="csharp" source="~/snippets/csharp/System/IFormatProvider/Overview/provider2.cs" interactive="try-dotnet" id="Snippet3":::
72+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormatProvider/Overview/provider2.fs" id="Snippet3":::
7273
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.IFormatProvider.Class/vb/provider2.vb" id="Snippet3":::
7374
7475
The following example illustrates the use of a class that implements the <xref:System.IFormatProvider> interface and the <xref:System.IFormatProvider.GetFormat%2A> method. The `AcctNumberFormat` class converts an <xref:System.Int64> value that represents an account number to a formatted 12-digit account number. Its `GetFormat` method returns a reference to the current `AcctNumberFormat` instance if the `formatType` parameter refers to a class that implements <xref:System.ICustomFormatter>; otherwise, `GetFormat` returns `null`.
7576
7677
:::code language="csharp" source="~/snippets/csharp/System/IFormatProvider/Overview/Provider.cs" id="Snippet2":::
78+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormatProvider/Overview/Provider.fs" id="Snippet2":::
7779
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.IFormatProvider.Class/vb/Provider.vb" id="Snippet2":::
7880
7981
The class that implements <xref:System.IFormatProvider> can then be used in a call to a formatting and parsing operation. For example, the following code calls the <xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType> method to generate a string that contains a formatted 12-digit account number.
8082
8183
:::code language="csharp" source="~/snippets/csharp/System/IFormatProvider/Overview/Provider.cs" id="Snippet1":::
84+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormatProvider/Overview/Provider.fs" id="Snippet1":::
8285
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.IFormatProvider.Class/vb/Provider.vb" id="Snippet1":::
8386
8487
]]></format>
@@ -148,11 +151,13 @@
148151
The following example illustrates the use of a class that implements the <xref:System.IFormatProvider> interface and the <xref:System.IFormatProvider.GetFormat%2A> method. The `AcctNumberFormat` class converts an <xref:System.Int64> value that represents an account number to a formatted 12-digit account number. Its `GetFormat` method returns a reference to itself if the `formatType` parameter refers to a class that implements <xref:System.ICustomFormatter>; otherwise, `GetFormat` returns `null`.
149152
150153
:::code language="csharp" source="~/snippets/csharp/System/IFormatProvider/Overview/Provider.cs" id="Snippet2":::
154+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormatProvider/Overview/Provider.fs" id="Snippet2":::
151155
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.IFormatProvider.Class/vb/Provider.vb" id="Snippet2":::
152156
153157
An instance of the `AcctNumberFormat` class can then be passed as an argument to a method that provides formatting or parsing services. For example, the following code passes an `AcctNumberFormat` class to the <xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType> method to generate a formatted 12-digit account number.
154158
155159
:::code language="csharp" source="~/snippets/csharp/System/IFormatProvider/Overview/Provider.cs" id="Snippet1":::
160+
:::code language="fsharp" source="~/snippets/fsharp/System/IFormatProvider/Overview/Provider.fs" id="Snippet1":::
156161
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.IFormatProvider.Class/vb/Provider.vb" id="Snippet1":::
157162
158163
]]></format>

0 commit comments

Comments
 (0)