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
18 changes: 18 additions & 0 deletions snippets/fsharp/System/Decimal/.ctor/ctor2a.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ctor2a

// <Snippet1>
open System

let values = [ 1234.96m; -1234.96m ]
for value in values do
let parts = Decimal.GetBits value
let sign = (parts[3] &&& 0x80000000) <> 0

let scale = (parts[3] >>> 16) &&& 0x7F |> byte
let newValue = Decimal(parts[0], parts[1], parts[2], sign, scale)
printfn $"{value} --> {newValue}"

// The example displays the following output:
// 1234.96 --> 1234.96
// -1234.96 --> -1234.96
// </Snippet1>
56 changes: 56 additions & 0 deletions snippets/fsharp/System/Decimal/.ctor/ctordo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module ctordo

//<Snippet2>
// Example of the decimal( double ) constructor.
open System

// Get the exception type name; remove the namespace prefix.
let getExceptionType (ex: exn) =
let exceptionType = ex.GetType() |> string
exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)

// Create a decimal object and display its value.
let createDecimal (value: double) valToStr =
// Format and display the constructor.
printf "%-34s" $"decimal( {valToStr} )"

try
// Construct the decimal value.
let decimalNum = Decimal value

// Display the value if it was created successfully.
printfn $"{decimalNum,31}"
with ex ->
// Display the exception type if an exception was thrown.
printfn $"{getExceptionType ex,31}"

printfn $"This example of the decimal( double ) constructor \ngenerates the following output.\n"
printfn "%-34s%31s" "Constructor" "Value or Exception"
printfn "%-34s%31s" "-----------" "------------------"

// Construct decimal objects from double values.
createDecimal 1.23456789E+5 "1.23456789E+5"
createDecimal 1.234567890123E+15 "1.234567890123E+15"
createDecimal 1.2345678901234567E+25 "1.2345678901234567E+25"
createDecimal 1.2345678901234567E+35 "1.2345678901234567E+35"
createDecimal 1.23456789E-5 "1.23456789E-5"
createDecimal 1.234567890123E-15 "1.234567890123E-15"
createDecimal 1.2345678901234567E-25 "1.2345678901234567E-25"
createDecimal 1.2345678901234567E-35 "1.2345678901234567E-35"
createDecimal (1. / 7.) "1. / 7."


// This example of the decimal( double ) constructor
// generates the following output.
//
// Constructor Value or Exception
// ----------- ------------------
// decimal( 1.23456789E+5 ) 123456.789
// decimal( 1.234567890123E+15 ) 1234567890123000
// decimal( 1.2345678901234567E+25 ) 12345678901234600000000000
// decimal( 1.2345678901234567E+35 ) OverflowException
// decimal( 1.23456789E-5 ) 0.0000123456789
// decimal( 1.234567890123E-15 ) 0.000000000000001234567890123
// decimal( 1.2345678901234567E-25 ) 0.0000000000000000000000001235
// decimal( 1.2345678901234567E-35 ) 0
//</Snippet2>
41 changes: 41 additions & 0 deletions snippets/fsharp/System/Decimal/.ctor/ctori.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module ctori

//<Snippet1>
// Example of the decimal(int) constructor.
open System

// Create a decimal object and display its value.
let createDecimal (value: int) valToStr =
let decimalNum = new decimal(value)

// Format the constructor for display.
let ctor = $"decimal( {valToStr} )"

// Display the constructor and its value.
printfn $"{ctor,-30}{decimalNum,16}"

printfn "This example of the decimal(int) constructor\ngenerates the following output.\n"
printfn "%-30s%16s" "Constructor" "Value"
printfn "%-30s%16s" "-----------" "-----"

// Construct decimal objects from int values.
createDecimal Int32.MinValue "Int32.MinValue"
createDecimal Int32.MaxValue "int.MaxValue"
createDecimal 0 "0"
createDecimal 999999999 "999999999"
createDecimal 0x40000000 "0x40000000"
createDecimal (int 0xC0000000) "int 0xC0000000"


// This example of the decimal(int) constructor
// generates the following output.

// Constructor Value
// ----------- -----
// decimal( Int32.MinValue ) -2147483648
// decimal( Int32.MaxValue ) 2147483647
// decimal( 0 ) 0
// decimal( 999999999 ) 999999999
// decimal( 0x40000000 ) 1073741824
// decimal( int 0xC0000000 ) -1073741824
//</Snippet1>
90 changes: 90 additions & 0 deletions snippets/fsharp/System/Decimal/.ctor/ctoriarr.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module ctoriarr

//<Snippet1>
// Example of the decimal(int[]) constructor.
open System

// Get the exception type name; remove the namespace prefix.
let getExceptionType (ex: exn) =
let exceptionType = ex.GetType() |> string
exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)

// Create a decimal object and display its value.
let createDecimal (bits: int[]) =
// Format the constructor for display.
let mutable ctor = String.Format("decimal( {{ 0x{0:X}", bits[0])

for i = 1 to bits.Length - 1 do
ctor <- $"{ctor}, 0x{bits[i]:X}"
ctor <- ctor + " } )"

let valOrExc =
try
// Construct the decimal value.
let decimalNum = new decimal(bits)

// Format the decimal value for display.
string decimalNum
with ex ->
// Save the exception type if an exception was thrown.
getExceptionType ex

// Display the constructor and decimal value or exception.
let ctorLen = 76 - valOrExc.Length;

// Display the data on one line if it will fit.
if ctorLen > ctor.Length then
printfn $"{ctor.PadRight ctorLen}{valOrExc}"

// Otherwise, display the data on two lines.
else
printfn $"{ctor}"
printfn $"{valOrExc,76}"

printfn
"""This example of the decimal(int[]) constructor
generates the following output.
"""
printfn "%-38s%38s" "Constructor" "Value or Exception"
printfn "%-38s%38s" "-----------" "------------------"

// Construct decimal objects from integer arrays.
createDecimal [| 0; 0; 0; 0 |]
createDecimal [| 0; 0; 0 |]
createDecimal [| 0; 0; 0; 0; 0 |]
createDecimal [| 1000000000; 0; 0; 0 |]
createDecimal [| 0; 1000000000; 0; 0 |]
createDecimal [| 0; 0; 1000000000; 0 |]
createDecimal [| 0; 0; 0; 1000000000 |]
createDecimal [| -1; -1; -1; 0 |]
createDecimal [| -1; -1; -1; 0x80000000 |]
createDecimal [| -1; 0; 0; 0x100000 |]
createDecimal [| -1; 0; 0; 0x1C0000 |]
createDecimal [| -1; 0; 0; 0x1D0000 |]
createDecimal [| -1; 0; 0; 0x1C0001 |]
createDecimal [| 0xF0000; 0xF0000; 0xF0000; 0xF0000 |]


// This example of the decimal(int[]) constructor
// generates the following output.
//
// Constructor Value or Exception
// ----------- ------------------
// decimal( { 0x0, 0x0, 0x0, 0x0 } ) 0
// decimal( { 0x0, 0x0, 0x0 } ) ArgumentException
// decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } ) ArgumentException
// decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } ) 1000000000
// decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } ) 4294967296000000000
// decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } ) 18446744073709551616000000000
// decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } ) ArgumentException
// decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
// 79228162514264337593543950335
// decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
// -79228162514264337593543950335
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } ) 0.0000004294967295
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } ) ArgumentException
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } ) ArgumentException
// decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
// 18133887298.441562272235520
//</Snippet1>
87 changes: 87 additions & 0 deletions snippets/fsharp/System/Decimal/.ctor/ctoriiibby.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module ctoriiibby

//<Snippet2>
// Example of the decimal( int, int, int, bool, byte ) constructor.
open System

// Get the exception type name; remove the namespace prefix.
let getExceptionType (ex: exn) =
let exceptionType = ex.GetType() |> string
exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)

// Create a decimal object and display its value.
let createDecimal low mid high (isNeg: bool) (scale: byte) =
// Format the constructor for display.
let ctor =
$"decimal( %i{low}, %i{mid}, %i{high}, {isNeg}, {scale} )"

let valOrExc =
try
// Construct the decimal value.
let decimalNum = new decimal(low, mid, high, isNeg, scale)

// Format and save the decimal value.
decimalNum |> string
with ex ->
// Save the exception type if an exception was thrown.
getExceptionType ex

// Display the constructor and decimal value or exception.
let ctorLen = 76 - valOrExc.Length

// Display the data on one line if it will fit.
if ctorLen > ctor.Length then
printfn $"{ctor.PadRight ctorLen}{valOrExc}"

// Otherwise, display the data on two lines.
else
printfn $"{ctor}"
printfn $"{valOrExc,76}"

printfn
"""This example of the decimal(int, int, int, bool, byte)
constructor generates the following output.
"""
printfn "%-38s%38s" "Constructor" "Value or Exception"
printfn "%-38s%38s" "-----------" "------------------"

// Construct decimal objects from the component fields.
createDecimal 0 0 0 false 0uy
createDecimal 0 0 0 false 27uy
createDecimal 0 0 0 true 0uy
createDecimal 1000000000 0 0 false 0uy
createDecimal 0 1000000000 0 false 0uy
createDecimal 0 0 1000000000 false 0uy
createDecimal 1000000000 1000000000 1000000000 false 0uy
createDecimal -1 -1 -1 false 0uy
createDecimal -1 -1 -1 true 0uy
createDecimal -1 -1 -1 false 15uy
createDecimal -1 -1 -1 false 28uy
createDecimal -1 -1 -1 false 29uy
createDecimal Int32.MaxValue 0 0 false 18uy
createDecimal Int32.MaxValue 0 0 false 28uy
createDecimal Int32.MaxValue 0 0 true 28uy


// This example of the decimal(int, int, int, bool, byte)
// constructor generates the following output.
//
// Constructor Value or Exception
// ----------- ------------------
// decimal( 0, 0, 0, False, 0 ) 0
// decimal( 0, 0, 0, False, 27 ) 0
// decimal( 0, 0, 0, True, 0 ) 0
// decimal( 1000000000, 0, 0, False, 0 ) 1000000000
// decimal( 0, 1000000000, 0, False, 0 ) 4294967296000000000
// decimal( 0, 0, 1000000000, False, 0 ) 18446744073709551616000000000
// decimal( 1000000000, 1000000000, 1000000000, False, 0 )
// 18446744078004518913000000000
// decimal( -1, -1, -1, False, 0 ) 79228162514264337593543950335
// decimal( -1, -1, -1, True, 0 ) -79228162514264337593543950335
// decimal( -1, -1, -1, False, 15 ) 79228162514264.337593543950335
// decimal( -1, -1, -1, False, 28 ) 7.9228162514264337593543950335
// decimal( -1, -1, -1, False, 29 ) ArgumentOutOfRangeException
// decimal( 2147483647, 0, 0, False, 18 ) 0.000000002147483647
// decimal( 2147483647, 0, 0, False, 28 ) 0.0000000000000000002147483647
// decimal( 2147483647, 0, 0, True, 28 ) -0.0000000000000000002147483647
//</Snippet2>
40 changes: 40 additions & 0 deletions snippets/fsharp/System/Decimal/.ctor/ctorl.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module ctorl

//<Snippet3>
// Example of the decimal( int64 ) constructor.
open System

// Create a decimal object and display its value.
let createDecimal (value: int64) valToStr =
let decimalNum = Decimal value

// Format the constructor for display.
let ctor = $"decimal( {valToStr} )"

// Display the constructor and its value.
printfn $"{ctor,-35}{decimalNum,22}"

printfn "This example of the decimal( int64 ) constructor\ngenerates the following output.\n"
printfn "%-35s%22s" "Constructor" "Value"
printfn "%-35s%22s" "-----------" "-----"

// Construct decimal objects from long values.
createDecimal Int64.MinValue "Int64.MinValue"
createDecimal Int64.MaxValue "Int64.MaxValue"
createDecimal 0L "0L"
createDecimal 999999999999999999L "999999999999999999L"
createDecimal 0x2000000000000000L "0x2000000000000000L"
createDecimal (int64 0xE000000000000000L) "int64 0xE000000000000000L"

// This example of the decimal( int64 ) constructor
// generates the following output.
//
// Constructor Value
// ----------- -----
// decimal( Int64.MinValue ) -9223372036854775808
// decimal( Int64.MaxValue ) 9223372036854775807
// decimal( 0L ) 0
// decimal( 999999999999999999L ) 999999999999999999
// decimal( 0x2000000000000000L ) 2305843009213693952
// decimal( int64 0xE000000000000000L ) -2305843009213693952
//</Snippet3>
Loading