Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="int64_equals.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// <Snippet1>
let myVariable1 = 80L
let myVariable2 = 80L

// Get and display the declaring type.
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is: {myVariable1}"
printfn $"\nType of 'myVariable2' is '{myVariable2.GetType()}' and value is: {myVariable2}"

// Compare 'myVariable1' instance with 'myVariable2' Object.
if myVariable1.Equals myVariable2 then
printfn "\nStructures 'myVariable1' and 'myVariable2' are equal"
else
printfn "\nStructures 'myVariable1' and 'myVariable2' are not equal"

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
open System
open System.Globalization

let callDefaultToString () =

// <Snippet1>
let value = -16325091L
// Display value using default ToString method.
printfn $"{value.ToString()}" // Displays -16325091
// Display value using some standard format specifiers.
printfn $"""{value.ToString "G"}""" // Displays -16325091
printfn $"""{value.ToString "C"}""" // Displays ($16,325,091.00)
printfn $"""{value.ToString "D"}""" // Displays -16325091
printfn $"""{value.ToString "F"}""" // Displays -16325091.00
printfn $"""{value.ToString "N"}""" // Displays -16,325,091.00
printfn $"""{value.ToString "N0"}""" // Displays -16,325,091
printfn $"""{value.ToString "X"}""" // Displays FFFFFFFFFF06E61D
// </Snippet1>

let callToStringWithSpecificCultures () =
// <Snippet2>
let value = -16325901L
// Display value using the invariant culture.
printfn $"{value.ToString CultureInfo.InvariantCulture}"
// Display value using the en-GB culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB" )}"""
// Display value using the de-DE culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE" )}"""
// This example displays the following output to the console:
// -16325901
// -16325901
// -16325901
// </Snippet2>


let callToStringWithSpecificSpecifiers () =
// <Snippet3>
let value = -16325L

// Use standard numeric format specifier.
let specifier = "G"
printfn $"{specifier}: {value.ToString specifier}"
// Displays: G: -16325

let specifier = "C"
printfn $"{specifier}: {value.ToString specifier}"
// Displays: C: ($16,325.00)

let specifier = "D8"
printfn $"{specifier}: {value.ToString specifier}"
// Displays: D8: -00016325

let specifier = "E4"
printfn $"{specifier}: {value.ToString specifier}"
// Displays: E4: -1.6325E+004

let specifier = "e3"
printfn $"{specifier}: {value.ToString specifier}"
// Displays: e3: -1.633e+004

let specifier = "F"
printfn $"{specifier}: {value.ToString specifier}"
// Displays: F: -16325.00

let specifier = "N"
printfn $"{specifier}: {value.ToString specifier }"
// Displays: N: -16,325.00

let specifier = "P"
printfn $"{specifier}: {(float value / 100000.0).ToString specifier}"
// Displays: P: -16.33 %

let specifier = "X"
printfn $"{specifier}: {value.ToString(specifier)}"
// Displays: X: FFFFFFFFFFFFC03B

// Use custom numeric format specifiers.
let specifier = "0,0.000"
printfn $"{specifier}: {value.ToString(specifier)}"
// Displays: 0,0.000: -16,325.000

let specifier = "#,#.00#(#,#.00#)"
printfn $"{specifier}: {(value * -1L).ToString specifier}"
// Displays: #,#.00#(#,#.00#): 16,325.00

// </Snippet3>

let callToStringWithSpecifiersAndCultures () =
// <Snippet4>
// Define cultures whose formatting conventions are to be used.
let cultures =
[| CultureInfo.CreateSpecificCulture "en-US"
CultureInfo.CreateSpecificCulture "fr-FR"
CultureInfo.CreateSpecificCulture "es-ES" |]

let positiveNumber = 1679L
let negativeNumber = -3045L
let specifiers = [| "G"; "C"; "D8"; "E2"; "F"; "N"; "N0"; "P"; "X8" |]

for specifier in specifiers do
for culture in cultures do
// Display values with "G" format specifier.
printfn $"{specifier} format using {culture.Name} culture: {positiveNumber.ToString(specifier, culture), 16} {negativeNumber.ToString(specifier, culture), 16}"
printfn ""

// The example displays the following output to the console:
// G format using en-US culture: 1679 -3045
// G format using fr-FR culture: 1679 -3045
// G format using es-ES culture: 1679 -3045
//
// C format using en-US culture: $1,679.00 ($3,045.00)
// C format using fr-FR culture: 1 679,00 € -3 045,00 €
// C format using es-ES culture: 1.679,00 € -3.045,00 €
//
// D8 format using en-US culture: 00001679 -00003045
// D8 format using fr-FR culture: 00001679 -00003045
// D8 format using es-ES culture: 00001679 -00003045
//
// E2 format using en-US culture: 1.68E+003 -3.05E+003
// E2 format using fr-FR culture: 1,68E+003 -3,05E+003
// E2 format using es-ES culture: 1,68E+003 -3,05E+003
//
// F format using en-US culture: 1679.00 -3045.00
// F format using fr-FR culture: 1679,00 -3045,00
// F format using es-ES culture: 1679,00 -3045,00
//
// N format using en-US culture: 1,679.00 -3,045.00
// N format using fr-FR culture: 1 679,00 -3 045,00
// N format using es-ES culture: 1.679,00 -3.045,00
//
// N0 format using en-US culture: 1,679 -3,045
// N0 format using fr-FR culture: 1 679 -3 045
// N0 format using es-ES culture: 1.679 -3.045
//
// P format using en-US culture: 167,900.00% -304,500.00%
// P format using fr-FR culture: 167 900,00 % -304 500,00 %
// P format using es-ES culture: 167.900,00 % -304.500,00 %
//
// X8 format using en-US culture: 0000068F FFFFFFFFFFFFF41B
// X8 format using fr-FR culture: 0000068F FFFFFFFFFFFFF41B
// X8 format using es-ES culture: 0000068F FFFFFFFFFFFFF41B
// </Snippet4>

callDefaultToString ()
printfn ""
callToStringWithSpecificCultures ()
printfn ""
callToStringWithSpecificSpecifiers ()
printfn ""
callToStringWithSpecifiersAndCultures ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="ToString.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module TryParse1

// <Snippet1>
open System

let tryToParse (value: string) =
match Int64.TryParse value with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
let value =
if isNull value then
""
else
value
printfn $"Attempted conversion of '{value}' failed."

tryToParse null
tryToParse "160519"
tryToParse "9432.0"
tryToParse "16,667"
tryToParse " -322 "
tryToParse "+4302"
tryToParse "(100);"
tryToParse "01FA"


// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '160519' to 160519.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100);' failed.
// Attempted conversion of '01FA' failed.
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module TryParse2

// <Snippet2>
open System
open System.Globalization

let callTryParse (stringToConvert: string) styles =
let provider =
// If currency symbol is allowed, use en-US culture.
if int (styles &&& NumberStyles.AllowCurrencySymbol) > 0 then
CultureInfo "en-US"
else
CultureInfo.InvariantCulture

match Int64.TryParse(stringToConvert, styles, provider) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."


[<EntryPoint>]
let main _ =
let numericString = "106779"
let styles = NumberStyles.Integer
callTryParse numericString styles

let numericString = "-30677"
let styles = NumberStyles.None
callTryParse numericString styles

let styles = NumberStyles.AllowLeadingSign
callTryParse numericString styles

let numericString = "301677-"
callTryParse numericString styles

let styles = styles ||| NumberStyles.AllowTrailingSign
callTryParse numericString styles

let numericString = "$10634"
let styles = NumberStyles.Integer
callTryParse numericString styles

let styles = NumberStyles.Integer ||| NumberStyles.AllowCurrencySymbol
callTryParse numericString styles

let numericString = "10345.00"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles

let numericString = "10345.72"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles

let numericString = "22,593"
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
callTryParse numericString styles

let numericString = "12E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles

let numericString = "12E03"
callTryParse numericString styles

let numericString = "80c1"
callTryParse numericString NumberStyles.HexNumber

let numericString = "0x80C1"
callTryParse numericString NumberStyles.HexNumber

0

// The example displays the following output to the console:
// Converted '106779' to 106779.
// Attempted conversion of '-30677' failed.
// Converted '-30677' to -30677.
// Attempted conversion of '301677-' failed.
// Converted '301677-' to -301677.
// Attempted conversion of '$10634' failed.
// Converted '$10634' to 10634.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to 32961.
// Attempted conversion of '0x80C1' failed.
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="TryParse1.fs" />
<Compile Include="TryParse2.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="toint64_1.fs" />
</ItemGroup>
</Project>
Loading