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
11 changes: 11 additions & 0 deletions snippets/fsharp/System/UInt16/TryParse/fs.fsproj
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="tryparse11.fs" />
<Compile Include="tryparse21.fs" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions snippets/fsharp/System/UInt16/TryParse/tryparse11.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module tryparse11

open System

// <Snippet1>
let numericStrings =
[ "1293.8"; "+1671.7"; "28347."
" 33113684 "; "(0)"; "-0"; "-1"
"+1293617"; "18-"; "119870"; "31,024"
" 3127094 "; "00700000" ]

for numericString in numericStrings do
match UInt32.TryParse numericString with
| true, number ->
printfn $"Converted '{numericString}' to {number}."
| _ ->
printfn $"Cannot convert '{numericString}' to a UInt32."
// The example displays the following output:
// Cannot convert '1293.8' to a UInt32.
// Cannot convert '+1671.7' to a UInt32.
// Cannot convert '28347.' to a UInt32.
// Converted ' 33113684 ' to 33113684.
// Cannot convert '(0)' to a UInt32.
// Converted '-0' to 0.
// Cannot convert '-1' to a UInt32.
// Converted '+1293617' to 1293617.
// Cannot convert '18-' to a UInt32.
// Converted '119870' to 119870.
// Cannot convert '31,024' to a UInt32.
// Converted ' 3127094 ' to 3127094.
// Converted '0070000' to 70000.
// </Snippet1>
57 changes: 57 additions & 0 deletions snippets/fsharp/System/UInt16/TryParse/tryparse21.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module tryparse21

// <Snippet2>
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match UInt32.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
printfn $"Attempted conversion of '{stringToConvert}' failed."
| _ -> ()

do
let numericString = "2106034"
let styles = NumberStyles.Integer
callTryParse numericString styles

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

let numericString = "29103674.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 = "41792210E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles

let numericString = "9112E-01"
callTryParse numericString styles

let numericString = "312E01"
callTryParse numericString styles

let numericString = "FFC86DA1"
callTryParse numericString NumberStyles.HexNumber

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

// The example displays the following output:
// Converted '2106034' to 2106034.
// Attempted conversion of '-10603' failed.
// Converted '29103674.00' to 29103674.
// Attempted conversion of '10345.72' failed.
// Converted '41792210E-01' to 4179221.
// Attempted conversion of '9112E-01' failed.
// Converted '312E01' to 3120.
// Converted 'FFC86DA1' to 4291325345.
// Attempted conversion of '0x8F8C' failed.
// </Snippet2>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/UInt32/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>
46 changes: 46 additions & 0 deletions snippets/fsharp/System/UInt32/CompareTo/source.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Snippets2

open System

//<snippet2>
type Temperature() =
// The value holder
let mutable m_value = 0u

static member MinValue =
UInt32.MinValue

static member MaxValue =
UInt32.MaxValue

member _.Value
with get () =
m_value
and set (v) =
m_value <- v
//</snippet2>

namespace Snippets3

open System
//<snippet3>
type Temperature() =
// The value holder
let mutable m_value = 0u

member _.Value
with get () =
m_value
and set (v) =
m_value <- v

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"

//</snippet3>
65 changes: 65 additions & 0 deletions snippets/fsharp/System/UInt32/Equals/equalsoverl.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module equalsoverl

// <Snippet1>
let value = 112u

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 long1 = 112L
printfn $"value = long1: {value.Equals long1,18}"
testObjectForEquality long1

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 ulong1 = 112uL
printfn $"value = ulong1: {value.Equals ulong1,18}"
testObjectForEquality ulong1

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 (UInt32) = 112 (Byte): False
//
// value = short1: False
// 112 (UInt32) = 112 (Int16): False
//
// value = long1: False
// 112 (UInt32) = 112 (Int64): False
//
// value = sbyte1: False
// 112 (UInt32) = 112 (SByte): False
//
// value = ushort1: True
// 112 (UInt32) = 112 (UInt16): False
//
// value = ulong1: False
// 112 (UInt32) = 112 (UInt64): False
//
// value = dec1: False
// 112 (UInt32) = 112 (Decimal): False
//
// value = dbl1: False
// 112 (UInt32) = 112 (Double): False
// </Snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/UInt32/Equals/fs.fsproj
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="uint32_equals.fs" />
<Compile Include="equalsoverl.fs" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions snippets/fsharp/System/UInt32/Equals/uint32_equals.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module uint32_equals
// <Snippet1>
let myVariable1 = 20u
let myVariable2 = 20u

// 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>
17 changes: 17 additions & 0 deletions snippets/fsharp/System/UInt32/MaxValue/MaxValue1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
open System

// <Snippet1>
let longValue = Int64.MaxValue / 2L

if longValue <= int64 UInt32.MaxValue && longValue >= int64 UInt32.MinValue then
let integerValue = uint longValue
printfn $"Converted long integer value to {integerValue:n0}."
else
let rangeLimit, relationship =
if longValue > int64 UInt32.MaxValue then
UInt32.MaxValue, "greater"
else
UInt32.MinValue, "less"

printfn $"Conversion failure: {longValue:n0} is {relationship} than {rangeLimit:n0}"
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/UInt32/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/UInt32/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>
33 changes: 33 additions & 0 deletions snippets/fsharp/System/UInt32/Parse/parse1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module parse1

// <Snippet1>
open System

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

for value in values do
try
let number = UInt32.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
// 2147483648 --> 2147483648
// 14065839182: Overflow
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034: Overflow
// </Snippet1>
Loading