1+ module parse_1
2+
3+ // <Snippet2>
4+ open System
5+ open System.Globalization
6+
7+ let provider = NumberFormatInfo.CurrentInfo
8+
9+ let callParseOperation stringValue ( style : NumberStyles ) =
10+ if stringValue = null then
11+ printfn " Cannot parse a null string..."
12+ else
13+ try
14+ let number = SByte.Parse( stringValue, style)
15+ printfn $" SByte.Parse('{stringValue}', {style})) = {number}"
16+ with
17+ | :? FormatException ->
18+ printfn $" '{stringValue}' and {style} throw a FormatException"
19+ | :? OverflowException ->
20+ printfn $" '{stringValue}' is outside the range of a signed byte"
21+
22+ [<EntryPoint>]
23+ let main _ =
24+ let stringValue = " 123 "
25+ let style = NumberStyles.None
26+ callParseOperation stringValue style
27+
28+ let stringValue = " 000,000,123"
29+ let style = NumberStyles.Integer ||| NumberStyles.AllowThousands
30+ callParseOperation stringValue style
31+
32+ let stringValue = " -100"
33+ let style = NumberStyles.AllowLeadingSign
34+ callParseOperation stringValue style
35+
36+ let stringValue = " 100-"
37+ let style = NumberStyles.AllowLeadingSign
38+ callParseOperation stringValue style
39+
40+ let stringValue = " 100-"
41+ let style = NumberStyles.AllowTrailingSign
42+ callParseOperation stringValue style
43+
44+ let stringValue = " $100"
45+ let style = NumberStyles.AllowCurrencySymbol
46+ callParseOperation stringValue style
47+
48+ let style = NumberStyles.Integer
49+ callParseOperation stringValue style
50+
51+ let style = NumberStyles.AllowDecimalPoint
52+ callParseOperation " 100.0" style
53+
54+ let stringValue = " 1e02"
55+ let style = NumberStyles.AllowExponent
56+ callParseOperation stringValue style
57+
58+ let stringValue = " (100)"
59+ let style = NumberStyles.AllowParentheses
60+ callParseOperation stringValue style
61+ 0
62+
63+ // The example displays the following information to the console:
64+ // ' 123 ' and None throw a FormatException
65+ // SByte.Parse('000,000,123', Integer, AllowThousands)) = 123
66+ // SByte.Parse('-100', AllowLeadingSign)) = -100
67+ // '100-' and AllowLeadingSign throw a FormatException
68+ // SByte.Parse('100-', AllowTrailingSign)) = -100
69+ // SByte.Parse('$100', AllowCurrencySymbol)) = 100
70+ // '$100' and Integer throw a FormatException
71+ // SByte.Parse('100.0', AllowDecimalPoint)) = 100
72+ // SByte.Parse('1e02', AllowExponent)) = 100
73+ // SByte.Parse('(100)', AllowParentheses)) = -100
74+ // </Snippet2>
0 commit comments