1+ module parseex2
2+
3+ // <Snippet6>
4+ open System
5+ open System.Globalization
6+
7+ let values = [| " 214 " ; " 1,064" ; " (0)" ; " 1241+" ; " + 214 " ; " +214 " ; " 2153.0" ; " 1e03" ; " 1300.0e-2" |]
8+ let whitespace = NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
9+ let styles =
10+ [| NumberStyles.None; whitespace
11+ NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign ||| whitespace
12+ NumberStyles.AllowThousands ||| NumberStyles.AllowCurrencySymbol
13+ NumberStyles.AllowExponent ||| NumberStyles.AllowDecimalPoint |]
14+
15+ // Attempt to convert each number using each style combination.
16+ for value in values do
17+ printfn $" Attempting to convert '{value}':"
18+ for style in styles do
19+ try
20+ let number = UInt16.Parse( value, style)
21+ printfn $" {style}: {number}"
22+ with :? FormatException ->
23+ printfn $" {style}: Bad Format"
24+ printfn " "
25+ // The example display the following output:
26+ // Attempting to convert ' 214 ':
27+ // None: Bad Format
28+ // AllowLeadingWhite, AllowTrailingWhite: 214
29+ // Integer, AllowTrailingSign: 214
30+ // AllowThousands, AllowCurrencySymbol: Bad Format
31+ // AllowDecimalPoint, AllowExponent: Bad Format
32+ //
33+ // Attempting to convert '1,064':
34+ // None: Bad Format
35+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
36+ // Integer, AllowTrailingSign: Bad Format
37+ // AllowThousands, AllowCurrencySymbol: 1064
38+ // AllowDecimalPoint, AllowExponent: Bad Format
39+ //
40+ // Attempting to convert '(0)':
41+ // None: Bad Format
42+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
43+ // Integer, AllowTrailingSign: Bad Format
44+ // AllowThousands, AllowCurrencySymbol: Bad Format
45+ // AllowDecimalPoint, AllowExponent: Bad Format
46+ //
47+ // Attempting to convert '1241+':
48+ // None: Bad Format
49+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
50+ // Integer, AllowTrailingSign: 1241
51+ // AllowThousands, AllowCurrencySymbol: Bad Format
52+ // AllowDecimalPoint, AllowExponent: Bad Format
53+ //
54+ // Attempting to convert ' + 214 ':
55+ // None: Bad Format
56+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
57+ // Integer, AllowTrailingSign: Bad Format
58+ // AllowThousands, AllowCurrencySymbol: Bad Format
59+ // AllowDecimalPoint, AllowExponent: Bad Format
60+ //
61+ // Attempting to convert ' +214 ':
62+ // None: Bad Format
63+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
64+ // Integer, AllowTrailingSign: 214
65+ // AllowThousands, AllowCurrencySymbol: Bad Format
66+ // AllowDecimalPoint, AllowExponent: Bad Format
67+ //
68+ // Attempting to convert '2153.0':
69+ // None: Bad Format
70+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
71+ // Integer, AllowTrailingSign: Bad Format
72+ // AllowThousands, AllowCurrencySymbol: Bad Format
73+ // AllowDecimalPoint, AllowExponent: 2153
74+ //
75+ // Attempting to convert '1e03':
76+ // None: Bad Format
77+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
78+ // Integer, AllowTrailingSign: Bad Format
79+ // AllowThousands, AllowCurrencySymbol: Bad Format
80+ // AllowDecimalPoint, AllowExponent: 1000
81+ //
82+ // Attempting to convert '1300.0e-2':
83+ // None: Bad Format
84+ // AllowLeadingWhite, AllowTrailingWhite: Bad Format
85+ // Integer, AllowTrailingSign: Bad Format
86+ // AllowThousands, AllowCurrencySymbol: Bad Format
87+ // AllowDecimalPoint, AllowExponent: 13
88+ // </Snippet6>
0 commit comments