Skip to content

Commit 925b31b

Browse files
authored
Int64 F# snippets (dotnet#7493)
1 parent 055d010 commit 925b31b

File tree

20 files changed

+1018
-2
lines changed

20 files changed

+1018
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="int64_equals.fs" />
8+
</ItemGroup>
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// <Snippet1>
2+
let myVariable1 = 80L
3+
let myVariable2 = 80L
4+
5+
// Get and display the declaring type.
6+
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is: {myVariable1}"
7+
printfn $"\nType of 'myVariable2' is '{myVariable2.GetType()}' and value is: {myVariable2}"
8+
9+
// Compare 'myVariable1' instance with 'myVariable2' Object.
10+
if myVariable1.Equals myVariable2 then
11+
printfn "\nStructures 'myVariable1' and 'myVariable2' are equal"
12+
else
13+
printfn "\nStructures 'myVariable1' and 'myVariable2' are not equal"
14+
15+
// </Snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
open System
2+
open System.Globalization
3+
4+
let callDefaultToString () =
5+
6+
// <Snippet1>
7+
let value = -16325091L
8+
// Display value using default ToString method.
9+
printfn $"{value.ToString()}" // Displays -16325091
10+
// Display value using some standard format specifiers.
11+
printfn $"""{value.ToString "G"}""" // Displays -16325091
12+
printfn $"""{value.ToString "C"}""" // Displays ($16,325,091.00)
13+
printfn $"""{value.ToString "D"}""" // Displays -16325091
14+
printfn $"""{value.ToString "F"}""" // Displays -16325091.00
15+
printfn $"""{value.ToString "N"}""" // Displays -16,325,091.00
16+
printfn $"""{value.ToString "N0"}""" // Displays -16,325,091
17+
printfn $"""{value.ToString "X"}""" // Displays FFFFFFFFFF06E61D
18+
// </Snippet1>
19+
20+
let callToStringWithSpecificCultures () =
21+
// <Snippet2>
22+
let value = -16325901L
23+
// Display value using the invariant culture.
24+
printfn $"{value.ToString CultureInfo.InvariantCulture}"
25+
// Display value using the en-GB culture.
26+
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB" )}"""
27+
// Display value using the de-DE culture.
28+
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE" )}"""
29+
// This example displays the following output to the console:
30+
// -16325901
31+
// -16325901
32+
// -16325901
33+
// </Snippet2>
34+
35+
36+
let callToStringWithSpecificSpecifiers () =
37+
// <Snippet3>
38+
let value = -16325L
39+
40+
// Use standard numeric format specifier.
41+
let specifier = "G"
42+
printfn $"{specifier}: {value.ToString specifier}"
43+
// Displays: G: -16325
44+
45+
let specifier = "C"
46+
printfn $"{specifier}: {value.ToString specifier}"
47+
// Displays: C: ($16,325.00)
48+
49+
let specifier = "D8"
50+
printfn $"{specifier}: {value.ToString specifier}"
51+
// Displays: D8: -00016325
52+
53+
let specifier = "E4"
54+
printfn $"{specifier}: {value.ToString specifier}"
55+
// Displays: E4: -1.6325E+004
56+
57+
let specifier = "e3"
58+
printfn $"{specifier}: {value.ToString specifier}"
59+
// Displays: e3: -1.633e+004
60+
61+
let specifier = "F"
62+
printfn $"{specifier}: {value.ToString specifier}"
63+
// Displays: F: -16325.00
64+
65+
let specifier = "N"
66+
printfn $"{specifier}: {value.ToString specifier }"
67+
// Displays: N: -16,325.00
68+
69+
let specifier = "P"
70+
printfn $"{specifier}: {(float value / 100000.0).ToString specifier}"
71+
// Displays: P: -16.33 %
72+
73+
let specifier = "X"
74+
printfn $"{specifier}: {value.ToString(specifier)}"
75+
// Displays: X: FFFFFFFFFFFFC03B
76+
77+
// Use custom numeric format specifiers.
78+
let specifier = "0,0.000"
79+
printfn $"{specifier}: {value.ToString(specifier)}"
80+
// Displays: 0,0.000: -16,325.000
81+
82+
let specifier = "#,#.00#(#,#.00#)"
83+
printfn $"{specifier}: {(value * -1L).ToString specifier}"
84+
// Displays: #,#.00#(#,#.00#): 16,325.00
85+
86+
// </Snippet3>
87+
88+
let callToStringWithSpecifiersAndCultures () =
89+
// <Snippet4>
90+
// Define cultures whose formatting conventions are to be used.
91+
let cultures =
92+
[| CultureInfo.CreateSpecificCulture "en-US"
93+
CultureInfo.CreateSpecificCulture "fr-FR"
94+
CultureInfo.CreateSpecificCulture "es-ES" |]
95+
96+
let positiveNumber = 1679L
97+
let negativeNumber = -3045L
98+
let specifiers = [| "G"; "C"; "D8"; "E2"; "F"; "N"; "N0"; "P"; "X8" |]
99+
100+
for specifier in specifiers do
101+
for culture in cultures do
102+
// Display values with "G" format specifier.
103+
printfn $"{specifier} format using {culture.Name} culture: {positiveNumber.ToString(specifier, culture), 16} {negativeNumber.ToString(specifier, culture), 16}"
104+
printfn ""
105+
106+
// The example displays the following output to the console:
107+
// G format using en-US culture: 1679 -3045
108+
// G format using fr-FR culture: 1679 -3045
109+
// G format using es-ES culture: 1679 -3045
110+
//
111+
// C format using en-US culture: $1,679.00 ($3,045.00)
112+
// C format using fr-FR culture: 1 679,00 € -3 045,00 €
113+
// C format using es-ES culture: 1.679,00 € -3.045,00 €
114+
//
115+
// D8 format using en-US culture: 00001679 -00003045
116+
// D8 format using fr-FR culture: 00001679 -00003045
117+
// D8 format using es-ES culture: 00001679 -00003045
118+
//
119+
// E2 format using en-US culture: 1.68E+003 -3.05E+003
120+
// E2 format using fr-FR culture: 1,68E+003 -3,05E+003
121+
// E2 format using es-ES culture: 1,68E+003 -3,05E+003
122+
//
123+
// F format using en-US culture: 1679.00 -3045.00
124+
// F format using fr-FR culture: 1679,00 -3045,00
125+
// F format using es-ES culture: 1679,00 -3045,00
126+
//
127+
// N format using en-US culture: 1,679.00 -3,045.00
128+
// N format using fr-FR culture: 1 679,00 -3 045,00
129+
// N format using es-ES culture: 1.679,00 -3.045,00
130+
//
131+
// N0 format using en-US culture: 1,679 -3,045
132+
// N0 format using fr-FR culture: 1 679 -3 045
133+
// N0 format using es-ES culture: 1.679 -3.045
134+
//
135+
// P format using en-US culture: 167,900.00% -304,500.00%
136+
// P format using fr-FR culture: 167 900,00 % -304 500,00 %
137+
// P format using es-ES culture: 167.900,00 % -304.500,00 %
138+
//
139+
// X8 format using en-US culture: 0000068F FFFFFFFFFFFFF41B
140+
// X8 format using fr-FR culture: 0000068F FFFFFFFFFFFFF41B
141+
// X8 format using es-ES culture: 0000068F FFFFFFFFFFFFF41B
142+
// </Snippet4>
143+
144+
callDefaultToString ()
145+
printfn ""
146+
callToStringWithSpecificCultures ()
147+
printfn ""
148+
callToStringWithSpecificSpecifiers ()
149+
printfn ""
150+
callToStringWithSpecifiersAndCultures ()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="ToString.fs" />
8+
</ItemGroup>
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module TryParse1
2+
3+
// <Snippet1>
4+
open System
5+
6+
let tryToParse (value: string) =
7+
match Int64.TryParse value with
8+
| true, number ->
9+
printfn $"Converted '{value}' to {number}."
10+
| _ ->
11+
let value =
12+
if isNull value then
13+
""
14+
else
15+
value
16+
printfn $"Attempted conversion of '{value}' failed."
17+
18+
tryToParse null
19+
tryToParse "160519"
20+
tryToParse "9432.0"
21+
tryToParse "16,667"
22+
tryToParse " -322 "
23+
tryToParse "+4302"
24+
tryToParse "(100);"
25+
tryToParse "01FA"
26+
27+
28+
// The example displays the following output to the console:
29+
// Attempted conversion of '' failed.
30+
// Converted '160519' to 160519.
31+
// Attempted conversion of '9432.0' failed.
32+
// Attempted conversion of '16,667' failed.
33+
// Converted ' -322 ' to -322.
34+
// Converted '+4302' to 4302.
35+
// Attempted conversion of '(100);' failed.
36+
// Attempted conversion of '01FA' failed.
37+
// </Snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module TryParse2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
let callTryParse (stringToConvert: string) styles =
8+
let provider =
9+
// If currency symbol is allowed, use en-US culture.
10+
if int (styles &&& NumberStyles.AllowCurrencySymbol) > 0 then
11+
CultureInfo "en-US"
12+
else
13+
CultureInfo.InvariantCulture
14+
15+
match Int64.TryParse(stringToConvert, styles, provider) with
16+
| true, number ->
17+
printfn $"Converted '{stringToConvert}' to {number}."
18+
| _ ->
19+
printfn $"Attempted conversion of '{stringToConvert}' failed."
20+
21+
22+
[<EntryPoint>]
23+
let main _ =
24+
let numericString = "106779"
25+
let styles = NumberStyles.Integer
26+
callTryParse numericString styles
27+
28+
let numericString = "-30677"
29+
let styles = NumberStyles.None
30+
callTryParse numericString styles
31+
32+
let styles = NumberStyles.AllowLeadingSign
33+
callTryParse numericString styles
34+
35+
let numericString = "301677-"
36+
callTryParse numericString styles
37+
38+
let styles = styles ||| NumberStyles.AllowTrailingSign
39+
callTryParse numericString styles
40+
41+
let numericString = "$10634"
42+
let styles = NumberStyles.Integer
43+
callTryParse numericString styles
44+
45+
let styles = NumberStyles.Integer ||| NumberStyles.AllowCurrencySymbol
46+
callTryParse numericString styles
47+
48+
let numericString = "10345.00"
49+
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
50+
callTryParse numericString styles
51+
52+
let numericString = "10345.72"
53+
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
54+
callTryParse numericString styles
55+
56+
let numericString = "22,593"
57+
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
58+
callTryParse numericString styles
59+
60+
let numericString = "12E-01"
61+
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
62+
callTryParse numericString styles
63+
64+
let numericString = "12E03"
65+
callTryParse numericString styles
66+
67+
let numericString = "80c1"
68+
callTryParse numericString NumberStyles.HexNumber
69+
70+
let numericString = "0x80C1"
71+
callTryParse numericString NumberStyles.HexNumber
72+
73+
0
74+
75+
// The example displays the following output to the console:
76+
// Converted '106779' to 106779.
77+
// Attempted conversion of '-30677' failed.
78+
// Converted '-30677' to -30677.
79+
// Attempted conversion of '301677-' failed.
80+
// Converted '301677-' to -301677.
81+
// Attempted conversion of '$10634' failed.
82+
// Converted '$10634' to 10634.
83+
// Converted '10345.00' to 10345.
84+
// Attempted conversion of '10345.72' failed.
85+
// Converted '22,593' to 22593.
86+
// Attempted conversion of '12E-01' failed.
87+
// Converted '12E03' to 12000.
88+
// Converted '80c1' to 32961.
89+
// Attempted conversion of '0x80C1' failed.
90+
// </Snippet2>
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+
<ItemGroup>
7+
<Compile Include="TryParse1.fs" />
8+
<Compile Include="TryParse2.fs" />
9+
</ItemGroup>
10+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="toint64_1.fs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)