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
10 changes: 10 additions & 0 deletions snippets/fsharp/System/UInt64/CompareTo/fs.fsproj
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="source.fs" />
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/UInt64/CompareTo/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//<snippet3>
open System

type Temperature() =
// The value holder
let mutable m_value = 0uL

interface IComparable with
/// IComparable.CompareTo implementation.
member _.CompareTo(obj) =
match obj with
| :? Temperature as temp ->
m_value.CompareTo temp.Value
| _ ->
invalidArg "obj" "object is not a Temperature"

member _.Value
with get () =
m_value
and set (v) =
m_value <- v
//</snippet3>
29 changes: 29 additions & 0 deletions snippets/fsharp/System/UInt64/Equals/equals1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module equals1

// <Snippet1>
let values: obj[] =
[| 10s; 20s; 10; 20
10L; 20L; 10.; 20.; 10us
20us; 10u; 20u
10uL; 20uL |]
let baseValue = 20uL
let baseType = baseValue.GetType().Name

for value in values do
printfn $"{baseValue} ({baseType}) = {value} ({value.GetType().Name}): {value.GetType().Name}"
// The example displays the following output:
// 20 (UInt64) = 10 (Int16): False
// 20 (UInt64) = 20 (Int16): False
// 20 (UInt64) = 10 (Int32): False
// 20 (UInt64) = 20 (Int32): False
// 20 (UInt64) = 10 (Int64): False
// 20 (UInt64) = 20 (Int64): False
// 20 (UInt64) = 10 (Double): False
// 20 (UInt64) = 20 (Double): False
// 20 (UInt64) = 10 (UInt16): False
// 20 (UInt64) = 20 (UInt16): False
// 20 (UInt64) = 10 (UInt32): False
// 20 (UInt64) = 20 (UInt32): False
// 20 (UInt64) = 10 (UInt64): False
// 20 (UInt64) = 20 (UInt64): True
// </Snippet1>
65 changes: 65 additions & 0 deletions snippets/fsharp/System/UInt64/Equals/equalsoverl.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module equalsoverl

// <Snippet2>
let value = 112uL

let testObjectForEquality (obj: obj) =
printfn $"{value} ({value.GetType().Name}) = {obj} ({obj.GetType().Name}): {value.Equals obj}\n"

let byte1= 112uy
printfn $"value = byte1: {value.Equals byte1,16}"
testObjectForEquality byte1

let short1 = 112s
printfn $"value = short1: {value.Equals short1,17}"
testObjectForEquality short1

let int1 = 112
printfn $"value = int1: {value.Equals int1,19}"
testObjectForEquality int1

let sbyte1 = 112y
printfn $"value = sbyte1: {value.Equals sbyte1,17}"
testObjectForEquality sbyte1

let ushort1 = 112us
printfn $"value = ushort1: {value.Equals ushort1,16}"
testObjectForEquality ushort1

let uint1 = 112u
printfn $"value = uint1: {value.Equals uint1,18}"
testObjectForEquality uint1

let dec1 = 112m
printfn $"value = dec1: {value.Equals dec1,21}"
testObjectForEquality dec1

let dbl1 = 112.
printfn $"value = dbl1: {value.Equals dbl1,20}"
testObjectForEquality dbl1

// The example displays the following output:
// value = byte1: True
// 112 (UInt64) = 112 (Byte): False
//
// value = short1: False
// 112 (UInt64) = 112 (Int16): False
//
// value = int1: False
// 112 (UInt64) = 112 (Int32): False
//
// value = sbyte1: False
// 112 (UInt64) = 112 (SByte): False
//
// value = ushort1: True
// 112 (UInt64) = 112 (UInt16): False
//
// value = uint1: True
// 112 (UInt64) = 112 (UInt32): False
//
// value = dec1: False
// 112 (UInt64) = 112 (Decimal): False
//
// value = dbl1: False
// 112 (UInt64) = 112 (Double): False
// </Snippet2>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/UInt64/Equals/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="equals1.fs" />
<Compile Include="equalsoverl.fs" />
<Compile Include="uint64_equals.fs" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions snippets/fsharp/System/UInt64/Equals/uint64_equals.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module uint64_equals

// <Snippet1>
let value1 = 50uL
let value2 = 50uL

// Display the values.
printfn $"value1: Type: {value1.GetType().Name} Value: {value1}"
printfn $"value2: Type: {value2.GetType().Name} Value: {value2}"

// Compare the two values.
printfn $"value1 and value2 are equal: {value1.Equals value2}"
// The example displays the following output:
// value1: Type: UInt64 Value: 50
// value2: Type: UInt64 Value: 50
// value1 and value2 are equal: True
// </Snippet1>
20 changes: 20 additions & 0 deletions snippets/fsharp/System/UInt64/MaxValue/MaxValue1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <Snippet1>
open System

let decimalValue = -1.5

// Discard fractional portion of Double value
let decimalInteger = floor decimalValue

if decimalInteger <= float UInt64.MaxValue && decimalInteger >= float UInt64.MinValue then
let integerValue = uint64 decimalValue
printfn $"Converted {decimalValue} to {integerValue}."
else
let rangeLimit, relationship =
if decimalInteger > float UInt64.MaxValue then
UInt64.MaxValue, "greater"
else
UInt64.MinValue, "less"

printfn $"Conversion failure: {decimalInteger} is {relationship} than {rangeLimit}."
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/UInt64/MaxValue/fs.fsproj
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="MaxValue1.fs" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions snippets/fsharp/System/UInt64/Parse/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="parse1.fs" />
<Compile Include="parseex2.fs" />
<Compile Include="parseex4.fs" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions snippets/fsharp/System/UInt64/Parse/parse1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module parse1

open System

// <Snippet1>
let values =
[| "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
"0xFA1B"; "163042"; "-10"; "14065839182"
"16e07"; "134985.0"; "-12034" |]
for value in values do
try
let number = UInt64.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: Overflow
// 14065839182 --> 14065839182
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034: Overflow
// </Snippet1>
93 changes: 93 additions & 0 deletions snippets/fsharp/System/UInt64/Parse/parseex2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
module parseex2

// <Snippet2>
open System
open System.Globalization

let values =
[| " 214309 "; "1,064,181"; "(0)"; "10241+"; " + 21499 "
" +21499 "; "122153.00"; "1e03ff"; "91300.0e-2" |]
let whitespace = NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
let styles =
[| NumberStyles.None; whitespace
NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign ||| whitespace
NumberStyles.AllowThousands ||| NumberStyles.AllowCurrencySymbol
NumberStyles.AllowExponent ||| NumberStyles.AllowDecimalPoint |]

// Attempt to convert each number using each style combination.
for value in values do
printfn $"Attempting to convert '{value}':"
for style in styles do
try
let number = UInt64.Parse(value, style)
printfn $" {style}: {number}"
with
| :? FormatException ->
printfn $" {style}: Bad Format"
| :? OverflowException ->
printfn $" {value}: Overflow"
printfn ""
// The example displays the following output:
// Attempting to convert ' 214309 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: 214309
// Integer, AllowTrailingSign: 214309
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '1,064,181':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064181
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '(0)':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '10241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 10241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' + 21499 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' +21499 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 21499
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '122153.00':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 122153
//
// Attempting to convert '1e03ff':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '91300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 913
// </Snippet2>
Loading