Skip to content

System.Int32 F# snippets #7453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2021
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="int32_equals.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// System.Int32.Equals(Object)

(*
The following program demonstrates the 'Equals(Object)' method
of struct 'Int32'. This compares an instance of 'Int32' with the
passed in object and returns true if they are equal.
*)

open System

// <Snippet1>
let myVariable1 = 60
let myVariable2 = 60

// Get and display the declaring type.
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is: {myVariable1}"
printfn $"Type 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,28 @@
// <Snippet1>
open System

type Comparison =
| LessThan = -1
| Equal = 0
| GreaterThan = 1

let mainValue = 16325
let zeroValue = 0
let negativeValue = -1934
let positiveValue = 903624
let sameValue = 16325

printfn $"Comparing {mainValue} and {zeroValue}: {mainValue.CompareTo zeroValue} ({enum<Comparison>(mainValue.CompareTo zeroValue)})."

printfn $"Comparing {mainValue} and {sameValue}: {mainValue.CompareTo sameValue} ({enum<Comparison>(mainValue.CompareTo sameValue)})."

printfn $"Comparing {mainValue} and {negativeValue}: {mainValue.CompareTo negativeValue} ({enum<Comparison>(mainValue.CompareTo negativeValue)})."

printfn $"Comparing {mainValue} and {positiveValue}: {mainValue.CompareTo positiveValue} ({enum<Comparison>(mainValue.CompareTo positiveValue)})."

// The example displays the following output:
// Comparing 16325 and 0: 1 (GreaterThan).
// Comparing 16325 and 16325: 0 (Equal).
// Comparing 16325 and -1934: 1 (GreaterThan).
// Comparing 16325 and 903624: -1 (LessThan).
// </Snippet1>
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="CompareTo1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
open System

let callToString () =
// <Snippet1>
let numbers = [| -1403; 0; 169; 1483104 |]
for number in numbers do
// Display value using default formatting.
printf $"{number,-8} --> "
// Display value with 3 digits and leading zeros.
printf $"{number,11:D3}"
// Display value with 1 decimal digit.
printf $"{number,13:N1}"
// Display value as hexadecimal.
printf $"{number,12:X2}"
// Display value with eight hexadecimal digits.
printfn $"{number,14:X8}"


// The example displays the following output:
// -1403 --> -1403 -1,403.0 FFFFFA85 FFFFFA85
// 0 --> 000 0.0 00 00000000
// 169 --> 169 169.0 A9 000000A9
// 1483104 --> 1483104 1,483,104.0 16A160 0016A160
// </Snippet1>


let callConvert () =
// <Snippet2>
let numbers = [| -146; 11043; 2781913 |]
printfn $"""{"Value",8} {"Binary",32} {"Octal",11} {"Hex",10}"""
for number in numbers do
printfn $"{number,8} {Convert.ToString(number, 2),32} {Convert.ToString(number, 8),11} {Convert.ToString(number, 16),10}"

// The example displays the following output:
// Value Binary Octal Hex
// -146 11111111111111111111111101101110 37777777556 ffffff6e
// 11043 10101100100011 25443 2b23
// 2781913 1010100111001011011001 12471331 2a72d9
// </Snippet2>

callToString ()
printfn "-----"
callConvert ()
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="Formatting1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
open System
open System.Numerics

let instantiateByAssignment () =
// <Snippet1>
let number1 = 64301
let number2 = 25548612
// </Snippet1>
printfn $"{number1} - {number2}"

let instantiateByNarrowingConversion () =
// <Snippet2>
let lNumber = 163245617L
try
let number1 = int lNumber
printfn $"{number1}"
with :? OverflowException ->
printfn "{lNumber} is out of range of an Int32."

let dbl2 = 35901.997
try
let number2 = int dbl2
printfn $"{number2}"
with :? OverflowException ->
printfn $"{dbl2} is out of range of an Int32."

let bigNumber = BigInteger 132451
try
let number3 = int bigNumber
printfn $"{number3}"
with :? OverflowException ->
printfn $"{bigNumber} is out of range of an Int32."

// The example displays the following output:
// 163245617
// 35902
// 132451
// </Snippet2>

let parse () =
// <Snippet3>
let string1 = "244681"
try
let number1 = Int32.Parse string1
printfn $"{number1}"
with
| :? OverflowException ->
printfn "'{string1}' is out of range of a 32-bit integer."
| :? FormatException ->
printfn $"The format of '{string1}' is invalid."

let string2 = "F9A3C"
try
let number2 = Int32.Parse(string2, System.Globalization.NumberStyles.HexNumber)
printfn $"{number2}"
with
| :? OverflowException ->
printfn $"'{string2}' is out of range of a 32-bit integer."
| :? FormatException ->
printfn $"The format of '{string2}' is invalid."

// The example displays the following output:
// 244681
// 1022524
// </Snippet3>

let instantiateByWideningConversion () =
// <Snippet4>
let value1 = 124y
let value2 = 1618s

let number1 = int value1
let number2 = int value2
// </Snippet4>
()

instantiateByAssignment ()
printfn "----"
instantiateByNarrowingConversion ()
printfn "----"
parse ()
printfn "----"
instantiateByWideningConversion ()

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="Instantiate1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Parse1

// <Snippet1>
open System

let values =
[ "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
"0xFA1B"; "163042"; "-10"; "007"; "2147483647"
"2147483648"; "16e07"; "134985.0"; "-12034"
"-2147483648"; "-2147483649" ]

for value in values do
try
let number = Int32.Parse value
printfn $"{value} --> {number}"
with
| :? FormatException ->
printfn $"{value}: Bad Format"
| :? OverflowException ->
printfn $"{value}: Overflow"


// The example displays the following output:
// +13230 --> 13230
// -0 --> 0
// 1,390,146: Bad Format
// $190,235,421,127: Bad Format
// 0xFA1B: Bad Format
// 163042 --> 163042
// -10 --> -10
// 007 --> 7
// 2147483647 --> 2147483647
// 2147483648: Overflow
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034 --> -12034
// -2147483648 --> -2147483648
// -2147483649: Overflow
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Parse2

// <Snippet2>
open System
open System.Globalization

let convert value (style: NumberStyles) =
try
let number = Int32.Parse(value, style)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int32 type."

convert "104.0" NumberStyles.AllowDecimalPoint
convert "104.9" NumberStyles.AllowDecimalPoint
convert " $17,198,064.42" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
convert "103E06" NumberStyles.AllowExponent
convert "-1,345,791" NumberStyles.AllowThousands
convert "(1,345,791)" (NumberStyles.AllowThousands ||| NumberStyles.AllowParentheses)


// The example displays the following output to the console:
// Converted '104.0' to 104.
// '104.9' is out of range of the Int32 type.
// ' $17,198,064.42' is out of range of the Int32 type.
// Converted '103E06' to 103000000.
// Unable to convert '-1,345,791'.
// Converted '(1,345,791)' to -1345791.
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Parse3

// <Snippet3>
open System
open System.Globalization

let convert (value: string) (style: NumberStyles) (provider: IFormatProvider) =
try
let number = Int32.Parse(value, style, provider)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int32 type."

convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "en-GB")
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "fr-FR")
convert "12,000" NumberStyles.Float (CultureInfo "en-US")
convert "12 425,00" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "sv-SE")
convert "12,425.00" (NumberStyles.Float ||| NumberStyles.AllowThousands) NumberFormatInfo.InvariantInfo
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "fr-FR")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "en-US")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowThousands) (CultureInfo "en-US")

// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int32 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Parse1.fs" />
<Compile Include="Parse2.fs" />
<Compile Include="Parse3.fs" />
</ItemGroup>
</Project>
Loading