Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4c85fa

Browse files
authored
System.Decimal F# snippets (#7605)
* Decimal F# snippets * fix build * Update ctors.fs * Move snippets * remove old snippets
1 parent 913b581 commit a4c85fa

File tree

105 files changed

+3301
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+3301
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module ctor2a
2+
3+
// <Snippet1>
4+
open System
5+
6+
let values = [ 1234.96m; -1234.96m ]
7+
for value in values do
8+
let parts = Decimal.GetBits value
9+
let sign = (parts[3] &&& 0x80000000) <> 0
10+
11+
let scale = (parts[3] >>> 16) &&& 0x7F |> byte
12+
let newValue = Decimal(parts[0], parts[1], parts[2], sign, scale)
13+
printfn $"{value} --> {newValue}"
14+
15+
// The example displays the following output:
16+
// 1234.96 --> 1234.96
17+
// -1234.96 --> -1234.96
18+
// </Snippet1>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module ctordo
2+
3+
//<Snippet2>
4+
// Example of the decimal( double ) constructor.
5+
open System
6+
7+
// Get the exception type name; remove the namespace prefix.
8+
let getExceptionType (ex: exn) =
9+
let exceptionType = ex.GetType() |> string
10+
exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)
11+
12+
// Create a decimal object and display its value.
13+
let createDecimal (value: double) valToStr =
14+
// Format and display the constructor.
15+
printf "%-34s" $"decimal( {valToStr} )"
16+
17+
try
18+
// Construct the decimal value.
19+
let decimalNum = Decimal value
20+
21+
// Display the value if it was created successfully.
22+
printfn $"{decimalNum,31}"
23+
with ex ->
24+
// Display the exception type if an exception was thrown.
25+
printfn $"{getExceptionType ex,31}"
26+
27+
printfn $"This example of the decimal( double ) constructor \ngenerates the following output.\n"
28+
printfn "%-34s%31s" "Constructor" "Value or Exception"
29+
printfn "%-34s%31s" "-----------" "------------------"
30+
31+
// Construct decimal objects from double values.
32+
createDecimal 1.23456789E+5 "1.23456789E+5"
33+
createDecimal 1.234567890123E+15 "1.234567890123E+15"
34+
createDecimal 1.2345678901234567E+25 "1.2345678901234567E+25"
35+
createDecimal 1.2345678901234567E+35 "1.2345678901234567E+35"
36+
createDecimal 1.23456789E-5 "1.23456789E-5"
37+
createDecimal 1.234567890123E-15 "1.234567890123E-15"
38+
createDecimal 1.2345678901234567E-25 "1.2345678901234567E-25"
39+
createDecimal 1.2345678901234567E-35 "1.2345678901234567E-35"
40+
createDecimal (1. / 7.) "1. / 7."
41+
42+
43+
// This example of the decimal( double ) constructor
44+
// generates the following output.
45+
//
46+
// Constructor Value or Exception
47+
// ----------- ------------------
48+
// decimal( 1.23456789E+5 ) 123456.789
49+
// decimal( 1.234567890123E+15 ) 1234567890123000
50+
// decimal( 1.2345678901234567E+25 ) 12345678901234600000000000
51+
// decimal( 1.2345678901234567E+35 ) OverflowException
52+
// decimal( 1.23456789E-5 ) 0.0000123456789
53+
// decimal( 1.234567890123E-15 ) 0.000000000000001234567890123
54+
// decimal( 1.2345678901234567E-25 ) 0.0000000000000000000000001235
55+
// decimal( 1.2345678901234567E-35 ) 0
56+
//</Snippet2>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module ctori
2+
3+
//<Snippet1>
4+
// Example of the decimal(int) constructor.
5+
open System
6+
7+
// Create a decimal object and display its value.
8+
let createDecimal (value: int) valToStr =
9+
let decimalNum = new decimal(value)
10+
11+
// Format the constructor for display.
12+
let ctor = $"decimal( {valToStr} )"
13+
14+
// Display the constructor and its value.
15+
printfn $"{ctor,-30}{decimalNum,16}"
16+
17+
printfn "This example of the decimal(int) constructor\ngenerates the following output.\n"
18+
printfn "%-30s%16s" "Constructor" "Value"
19+
printfn "%-30s%16s" "-----------" "-----"
20+
21+
// Construct decimal objects from int values.
22+
createDecimal Int32.MinValue "Int32.MinValue"
23+
createDecimal Int32.MaxValue "int.MaxValue"
24+
createDecimal 0 "0"
25+
createDecimal 999999999 "999999999"
26+
createDecimal 0x40000000 "0x40000000"
27+
createDecimal (int 0xC0000000) "int 0xC0000000"
28+
29+
30+
// This example of the decimal(int) constructor
31+
// generates the following output.
32+
33+
// Constructor Value
34+
// ----------- -----
35+
// decimal( Int32.MinValue ) -2147483648
36+
// decimal( Int32.MaxValue ) 2147483647
37+
// decimal( 0 ) 0
38+
// decimal( 999999999 ) 999999999
39+
// decimal( 0x40000000 ) 1073741824
40+
// decimal( int 0xC0000000 ) -1073741824
41+
//</Snippet1>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module ctoriarr
2+
3+
//<Snippet1>
4+
// Example of the decimal(int[]) constructor.
5+
open System
6+
7+
// Get the exception type name; remove the namespace prefix.
8+
let getExceptionType (ex: exn) =
9+
let exceptionType = ex.GetType() |> string
10+
exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)
11+
12+
// Create a decimal object and display its value.
13+
let createDecimal (bits: int[]) =
14+
// Format the constructor for display.
15+
let mutable ctor = String.Format("decimal( {{ 0x{0:X}", bits[0])
16+
17+
for i = 1 to bits.Length - 1 do
18+
ctor <- $"{ctor}, 0x{bits[i]:X}"
19+
ctor <- ctor + " } )"
20+
21+
let valOrExc =
22+
try
23+
// Construct the decimal value.
24+
let decimalNum = new decimal(bits)
25+
26+
// Format the decimal value for display.
27+
string decimalNum
28+
with ex ->
29+
// Save the exception type if an exception was thrown.
30+
getExceptionType ex
31+
32+
// Display the constructor and decimal value or exception.
33+
let ctorLen = 76 - valOrExc.Length;
34+
35+
// Display the data on one line if it will fit.
36+
if ctorLen > ctor.Length then
37+
printfn $"{ctor.PadRight ctorLen}{valOrExc}"
38+
39+
// Otherwise, display the data on two lines.
40+
else
41+
printfn $"{ctor}"
42+
printfn $"{valOrExc,76}"
43+
44+
printfn
45+
"""This example of the decimal(int[]) constructor
46+
generates the following output.
47+
"""
48+
printfn "%-38s%38s" "Constructor" "Value or Exception"
49+
printfn "%-38s%38s" "-----------" "------------------"
50+
51+
// Construct decimal objects from integer arrays.
52+
createDecimal [| 0; 0; 0; 0 |]
53+
createDecimal [| 0; 0; 0 |]
54+
createDecimal [| 0; 0; 0; 0; 0 |]
55+
createDecimal [| 1000000000; 0; 0; 0 |]
56+
createDecimal [| 0; 1000000000; 0; 0 |]
57+
createDecimal [| 0; 0; 1000000000; 0 |]
58+
createDecimal [| 0; 0; 0; 1000000000 |]
59+
createDecimal [| -1; -1; -1; 0 |]
60+
createDecimal [| -1; -1; -1; 0x80000000 |]
61+
createDecimal [| -1; 0; 0; 0x100000 |]
62+
createDecimal [| -1; 0; 0; 0x1C0000 |]
63+
createDecimal [| -1; 0; 0; 0x1D0000 |]
64+
createDecimal [| -1; 0; 0; 0x1C0001 |]
65+
createDecimal [| 0xF0000; 0xF0000; 0xF0000; 0xF0000 |]
66+
67+
68+
// This example of the decimal(int[]) constructor
69+
// generates the following output.
70+
//
71+
// Constructor Value or Exception
72+
// ----------- ------------------
73+
// decimal( { 0x0, 0x0, 0x0, 0x0 } ) 0
74+
// decimal( { 0x0, 0x0, 0x0 } ) ArgumentException
75+
// decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } ) ArgumentException
76+
// decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } ) 1000000000
77+
// decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } ) 4294967296000000000
78+
// decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } ) 18446744073709551616000000000
79+
// decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } ) ArgumentException
80+
// decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
81+
// 79228162514264337593543950335
82+
// decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
83+
// -79228162514264337593543950335
84+
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } ) 0.0000004294967295
85+
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
86+
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } ) ArgumentException
87+
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } ) ArgumentException
88+
// decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
89+
// 18133887298.441562272235520
90+
//</Snippet1>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module ctoriiibby
2+
3+
//<Snippet2>
4+
// Example of the decimal( int, int, int, bool, byte ) constructor.
5+
open System
6+
7+
// Get the exception type name; remove the namespace prefix.
8+
let getExceptionType (ex: exn) =
9+
let exceptionType = ex.GetType() |> string
10+
exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)
11+
12+
// Create a decimal object and display its value.
13+
let createDecimal low mid high (isNeg: bool) (scale: byte) =
14+
// Format the constructor for display.
15+
let ctor =
16+
$"decimal( %i{low}, %i{mid}, %i{high}, {isNeg}, {scale} )"
17+
18+
let valOrExc =
19+
try
20+
// Construct the decimal value.
21+
let decimalNum = new decimal(low, mid, high, isNeg, scale)
22+
23+
// Format and save the decimal value.
24+
decimalNum |> string
25+
with ex ->
26+
// Save the exception type if an exception was thrown.
27+
getExceptionType ex
28+
29+
// Display the constructor and decimal value or exception.
30+
let ctorLen = 76 - valOrExc.Length
31+
32+
// Display the data on one line if it will fit.
33+
if ctorLen > ctor.Length then
34+
printfn $"{ctor.PadRight ctorLen}{valOrExc}"
35+
36+
// Otherwise, display the data on two lines.
37+
else
38+
printfn $"{ctor}"
39+
printfn $"{valOrExc,76}"
40+
41+
printfn
42+
"""This example of the decimal(int, int, int, bool, byte)
43+
constructor generates the following output.
44+
"""
45+
printfn "%-38s%38s" "Constructor" "Value or Exception"
46+
printfn "%-38s%38s" "-----------" "------------------"
47+
48+
// Construct decimal objects from the component fields.
49+
createDecimal 0 0 0 false 0uy
50+
createDecimal 0 0 0 false 27uy
51+
createDecimal 0 0 0 true 0uy
52+
createDecimal 1000000000 0 0 false 0uy
53+
createDecimal 0 1000000000 0 false 0uy
54+
createDecimal 0 0 1000000000 false 0uy
55+
createDecimal 1000000000 1000000000 1000000000 false 0uy
56+
createDecimal -1 -1 -1 false 0uy
57+
createDecimal -1 -1 -1 true 0uy
58+
createDecimal -1 -1 -1 false 15uy
59+
createDecimal -1 -1 -1 false 28uy
60+
createDecimal -1 -1 -1 false 29uy
61+
createDecimal Int32.MaxValue 0 0 false 18uy
62+
createDecimal Int32.MaxValue 0 0 false 28uy
63+
createDecimal Int32.MaxValue 0 0 true 28uy
64+
65+
66+
// This example of the decimal(int, int, int, bool, byte)
67+
// constructor generates the following output.
68+
//
69+
// Constructor Value or Exception
70+
// ----------- ------------------
71+
// decimal( 0, 0, 0, False, 0 ) 0
72+
// decimal( 0, 0, 0, False, 27 ) 0
73+
// decimal( 0, 0, 0, True, 0 ) 0
74+
// decimal( 1000000000, 0, 0, False, 0 ) 1000000000
75+
// decimal( 0, 1000000000, 0, False, 0 ) 4294967296000000000
76+
// decimal( 0, 0, 1000000000, False, 0 ) 18446744073709551616000000000
77+
// decimal( 1000000000, 1000000000, 1000000000, False, 0 )
78+
// 18446744078004518913000000000
79+
// decimal( -1, -1, -1, False, 0 ) 79228162514264337593543950335
80+
// decimal( -1, -1, -1, True, 0 ) -79228162514264337593543950335
81+
// decimal( -1, -1, -1, False, 15 ) 79228162514264.337593543950335
82+
// decimal( -1, -1, -1, False, 28 ) 7.9228162514264337593543950335
83+
// decimal( -1, -1, -1, False, 29 ) ArgumentOutOfRangeException
84+
// decimal( 2147483647, 0, 0, False, 18 ) 0.000000002147483647
85+
// decimal( 2147483647, 0, 0, False, 28 ) 0.0000000000000000002147483647
86+
// decimal( 2147483647, 0, 0, True, 28 ) -0.0000000000000000002147483647
87+
//</Snippet2>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module ctorl
2+
3+
//<Snippet3>
4+
// Example of the decimal( int64 ) constructor.
5+
open System
6+
7+
// Create a decimal object and display its value.
8+
let createDecimal (value: int64) valToStr =
9+
let decimalNum = Decimal value
10+
11+
// Format the constructor for display.
12+
let ctor = $"decimal( {valToStr} )"
13+
14+
// Display the constructor and its value.
15+
printfn $"{ctor,-35}{decimalNum,22}"
16+
17+
printfn "This example of the decimal( int64 ) constructor\ngenerates the following output.\n"
18+
printfn "%-35s%22s" "Constructor" "Value"
19+
printfn "%-35s%22s" "-----------" "-----"
20+
21+
// Construct decimal objects from long values.
22+
createDecimal Int64.MinValue "Int64.MinValue"
23+
createDecimal Int64.MaxValue "Int64.MaxValue"
24+
createDecimal 0L "0L"
25+
createDecimal 999999999999999999L "999999999999999999L"
26+
createDecimal 0x2000000000000000L "0x2000000000000000L"
27+
createDecimal (int64 0xE000000000000000L) "int64 0xE000000000000000L"
28+
29+
// This example of the decimal( int64 ) constructor
30+
// generates the following output.
31+
//
32+
// Constructor Value
33+
// ----------- -----
34+
// decimal( Int64.MinValue ) -9223372036854775808
35+
// decimal( Int64.MaxValue ) 9223372036854775807
36+
// decimal( 0L ) 0
37+
// decimal( 999999999999999999L ) 999999999999999999
38+
// decimal( 0x2000000000000000L ) 2305843009213693952
39+
// decimal( int64 0xE000000000000000L ) -2305843009213693952
40+
//</Snippet3>

0 commit comments

Comments
 (0)