Skip to content

Commit 209e682

Browse files
authored
SByte F# snippets (#7836)
1 parent 79c9e22 commit 209e682

File tree

16 files changed

+501
-0
lines changed

16 files changed

+501
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
open System
2+
3+
// <Snippet1>
4+
let longValue = -130L
5+
6+
if longValue <= int64 SByte.MaxValue && longValue >= int64 SByte.MinValue then
7+
let byteValue = int8 longValue
8+
printfn $"Converted long integer value to {byteValue}."
9+
else
10+
let rangeLimit, relationship =
11+
if longValue > int64 SByte.MaxValue then
12+
SByte.MaxValue, "greater"
13+
else
14+
SByte.MinValue, "less"
15+
16+
printfn $"Conversion failure: {longValue:n0} is {relationship} than {rangeLimit}."
17+
// </Snippet1>
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="MaxValue1.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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="parseex1.fs" />
9+
<Compile Include="parseex2.fs" />
10+
<Compile Include="parseex3.fs" />
11+
<Compile Include="parse_1.fs" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module parseex1
2+
3+
// <Snippet1>
4+
open System
5+
6+
// Define an array of numeric strings.
7+
let values =
8+
[| "-16"; " -3"; "+ 12"; " +12 "; " 12 "
9+
"+120"; "(103)"; "192"; "-160" |]
10+
11+
// Parse each string and display the result.
12+
for value in values do
13+
try
14+
printfn $"Converted '{value}' to the SByte value {SByte.Parse value}."
15+
with
16+
| :? FormatException ->
17+
printfn $"'{value}' cannot be parsed successfully by SByte type."
18+
| :? OverflowException ->
19+
printfn $"'{value}' is out of range of the SByte type."
20+
21+
// The example displays the following output:
22+
// Converted '-16' to the SByte value -16.
23+
// Converted ' -3' to the SByte value -3.
24+
// '+ 12' cannot be parsed successfully by SByte type.
25+
// Converted ' +12 ' to the SByte value 12.
26+
// Converted ' 12 ' to the SByte value 12.
27+
// Converted '+120' to the SByte value 120.
28+
// '(103)' cannot be parsed successfully by SByte type.
29+
// '192' is out of range of the SByte type.
30+
// '-160' is out of range of the SByte type.
31+
// </Snippet1>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module parseex2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
// Parse value with no styles allowed.
8+
let values1 = [| " 121 "; "121"; "-121" |]
9+
let style = NumberStyles.None
10+
printfn $"Styles: {style}"
11+
for value in values1 do
12+
try
13+
let number = SByte.Parse(value, style)
14+
printfn $" Converted '{value}' to {number}."
15+
with :? FormatException ->
16+
printfn $" Unable to parse '{value}'."
17+
printfn ""
18+
19+
// Parse value with trailing sign.
20+
let style2 = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
21+
let values2 = [| " 103+"; " 103 +"; "+103"; "(103)"; " +103 " |]
22+
printfn $"Styles: {style2}"
23+
for value in values2 do
24+
try
25+
let number = SByte.Parse(value, style2)
26+
printfn $" Converted '{value}' to {number}."
27+
with
28+
| :? FormatException ->
29+
printfn $" Unable to parse '{value}'."
30+
| :? OverflowException ->
31+
printfn $" '{value}' is out of range of the SByte type."
32+
printfn ""
33+
// The example displays the following output:
34+
// Styles: None
35+
// Unable to parse ' 121 '.
36+
// Converted '121' to 121.
37+
// Unable to parse '-121'.
38+
//
39+
// Styles: Integer, AllowTrailingSign
40+
// Converted ' 103+' to 103.
41+
// Converted ' 103 +' to 103.
42+
// Converted '+103' to 103.
43+
// Unable to parse '(103)'.
44+
// Converted ' +103 ' to 103.
45+
// </Snippet2>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module parseex3
2+
3+
// <Snippet3>
4+
open System
5+
open System.Globalization
6+
7+
let nf = NumberFormatInfo()
8+
nf.NegativeSign <- "~"
9+
10+
let values = [| "-103"; "+12"; "~16"; " 1"; "~255" |]
11+
let providers: IFormatProvider[] = [| nf; CultureInfo.InvariantCulture |]
12+
13+
for provider in providers do
14+
printfn $"Conversions using {(box provider).GetType().Name}:"
15+
for value in values do
16+
try
17+
printfn $" Converted '{value}' to {SByte.Parse(value, provider)}."
18+
with
19+
| :? FormatException ->
20+
printfn $" Unable to parse '{value}'."
21+
| :? OverflowException ->
22+
printfn $" '{value}' is out of range of the SByte type."
23+
24+
// The example displays the following output:
25+
// Conversions using NumberFormatInfo:
26+
// Unable to parse '-103'.
27+
// Converted '+12' to 12.
28+
// Converted '~16' to -16.
29+
// Converted ' 1' to 1.
30+
// '~255' is out of range of the SByte type.
31+
// Conversions using CultureInfo:
32+
// Converted '-103' to -103.
33+
// Converted '+12' to 12.
34+
// Unable to parse '~16'.
35+
// Converted ' 1' to 1.
36+
// Unable to parse '~255'.
37+
// </Snippet3>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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="tostring2.fs" />
9+
<Compile Include="tostring3.fs" />
10+
<Compile Include="tostring4.fs" />
11+
<Compile Include="tostring5.fs" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module tostring2
2+
3+
// <Snippet2>
4+
let value = -123y
5+
// Display value using default ToString method.
6+
printfn $"{value.ToString()}" // Displays -123
7+
// Display value using some standard format specifiers.
8+
printfn $"""{value.ToString "G"}""" // Displays -123
9+
printfn $"""{value.ToString "C"}""" // Displays ($-123.00)
10+
printfn $"""{value.ToString "D"}""" // Displays -123
11+
printfn $"""{value.ToString "F"}""" // Displays -123.00
12+
printfn $"""{value.ToString "N"}""" // Displays -123.00
13+
printfn $"""{value.ToString "X"}""" // Displays 85
14+
// </Snippet2>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module tostring3
2+
3+
// <Snippet3>
4+
open System.Globalization
5+
6+
// Define a custom NumberFormatInfo object with "~" as its negative sign.
7+
let nfi = NumberFormatInfo(NegativeSign = "~")
8+
9+
// Initialize an array of SByte values.
10+
let bytes = [| -122y; 17y; 124y |]
11+
12+
// Display the formatted result using the custom provider.
13+
printfn "Using the custom NumberFormatInfo object:"
14+
for value in bytes do
15+
printfn $"{value.ToString nfi}"
16+
17+
printfn ""
18+
19+
// Display the formatted result using the invariant culture.
20+
printfn "Using the invariant culture:"
21+
for value in bytes do
22+
printfn $"{value.ToString NumberFormatInfo.InvariantInfo}"
23+
// The example displays the following output:
24+
// Using the custom NumberFormatInfo object:
25+
// ~122
26+
// 17
27+
// 124
28+
//
29+
// Using the invariant culture:
30+
// -122
31+
// 17
32+
// 124
33+
// </Snippet3>

0 commit comments

Comments
 (0)