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
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="int16_equals.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// System.Int16.Equals(Object)

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

// <Snippet1>
let myVariable1 = 20s
let myVariable2 = 20s

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

let numbersToConvert = [ 162345L; 32183L; -54000L ]

for number in numbersToConvert do
if number >= int64 Int16.MinValue && number <= int64 Int16.MaxValue then
let newNumber = Convert.ToInt16 number
printfn $"Successfully converted {newNumber} to an Int16."
else
printfn $"Unable to convert {number} to an Int16."

// The example displays the following output to the console:
// Unable to convert 162345 to an Int16.
// Successfully converted 32183 to an Int16.
// Unable to convert -54000 to an Int16.
// </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="MaxValue.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
module Parse

open System
open System.Globalization


let callParse1 () =
// <Snippet1>
let value = " 12603 "
try
let number = Int16.Parse value
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn $"Unable to convert '{value}' to a 16-bit signed integer."

let value = " 16,054"
try
let number = Int16.Parse value
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn "Unable to convert '{value}' to a 16-bit signed integer."

let value = " -17264"
try
let number = Int16.Parse value
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn "Unable to convert '{value}' to a 16-bit signed integer."


// The example displays the following output to the console:
// Converted ' 12603 ' to 12603.
// Unable to convert ' 16,054' to a 16-bit signed integer.
// Converted ' -17264' to -17264.
// </Snippet1>

let callParse3 () =
// <Snippet3>
// Parse string using "." as the thousands separator
// and " " as the decimal separator.
let value = "19 694,00"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let provider = CultureInfo "fr-FR"

let number = Int16.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
// '19 694,00' converted to 19694.

try
let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '19 694,00'.

// Parse string using "$" as the currency symbol for en_GB and
// en-US cultures.
let value = "$6,032.00"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol

try
let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '$6,032.00'.

let provider = CultureInfo "en-US"
let number = Int16.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
// '$6,032.00' converted to 6032.
// </Snippet3>


let callParse4 () =
// <Snippet4>
let stringToConvert = " 214 "
try
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is out of range of the Int16 data type."

let stringToConvert = " + 214"
try
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is out of range of the Int16 data type."

let stringToConvert = " +214 "
try
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is out of range of the Int16 data type."

// The example displays the following output to the console:
// Converted ' 214 ' to 214.
// Unable to parse ' + 214'.
// Converted ' +214 ' to 214.
// </Snippet4>


callParse1 ()
printfn "-----"
callParse3 ()
printfn "-----"
callParse4 ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <Snippet2>
open System
open System.Globalization

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

[<EntryPoint>]
let main _ =
// Parse a number with a thousands separator (throws an exception).
let value = "14,644"
let style = NumberStyles.None
parseToInt16 value style

let style = NumberStyles.AllowThousands
parseToInt16 value style

// Parse a number with a thousands separator and decimal point.
let value = "14,644.00"
let style = NumberStyles.AllowThousands ||| NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
parseToInt16 value style

// Parse a number with a fractional component (throws an exception).
let value = "14,644.001"
parseToInt16 value style

// Parse a number in exponential notation.
let value = "145E02"
let style = style ||| NumberStyles.AllowExponent
parseToInt16 value style

// Parse a number in exponential notation with a positive sign.
let value = "145E+02"
parseToInt16 value style

// Parse a number in exponential notation with a negative sign
// (throws an exception).
let value = "145E-02"
parseToInt16 value style

0

// The example displays the following output to the console:
// Unable to parse '14,644' with style None.
// Converted '14,644' to 14644.
// Converted '14,644.00' to 14644.
// '14,644.001' is out of range of the Int16 type.
// Converted '145E02' to 14500.
// Converted '145E+02' to 14500.
// '145E-02' is out of range of the Int16 type.
// </Snippet2>
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="Parse.fs" />
<Compile Include="Parse2.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
open System
open System.Globalization

let callDefaultToString () =
// <Snippet1>
let numbers = [ 0s; 14624s; 13982s; Int16.MaxValue; Int16.MinValue; -16667s ]

for number in numbers do
printfn $"{number.ToString()}"

// The example displays the following output to the console:
// 0
// 14624
// 13982
// 32767
// -32768
// -16667
// </Snippet1>

let callToStringWithProvider () =
// <Snippet2>
let numbers = [ -23092s; 0s; 14894s; Int16.MaxValue ]
let providers =
[ CultureInfo "en-us"
CultureInfo "fr-fr"
CultureInfo "de-de"
CultureInfo "es-es" ]

for int16Value in numbers do
for provider in providers do
printf $"{int16Value.ToString provider, 6} ({provider.Name}) "

printfn ""

// The example displays the following output to the console:
// -23092 (en-US) -23092 (fr-FR) -23092 (de-DE) -23092 (es-ES)
// 0 (en-US) 0 (fr-FR) 0 (de-DE) 0 (es-ES)
// 14894 (en-US) 14894 (fr-FR) 14894 (de-DE) 14894 (es-ES)
// 32767 (en-US) 32767 (fr-FR) 32767 (de-DE) 32767 (es-ES)
// </Snippet2>

let callToStringWithSpecifiers () =
// <Snippet3>
let values = [| -23805s; 32194s |]
let formats =
[ "C4"; "D6"; "e1"; "E2"; "F1"; "G"; "N1"
"P0"; "X4"; "000000.0000"; "##000.0" ]
for format in formats do
printfn $"'{format,2}' format specifier: {values[0].ToString format,17} {values[1].ToString format,17}"

// The example displays the following output to the console:
// 'C4' format specifier: ($23,805.0000) $32,194.0000
// 'D6' format specifier: -023805 032194
// 'e1' format specifier: -2.4e+004 3.2e+004
// 'E2' format specifier: -2.38E+004 3.22E+004
// 'F1' format specifier: -23805.0 32194.0
// ' G' format specifier: -23805 32194
// 'N1' format specifier: -23,805.0 32,194.0
// 'P0' format specifier: -2,380,500 % 3,219,400 %
// 'X4' format specifier: A303 7DC2
// '000000.0000' format specifier: -023805.0000 032194.0000
// '##000.0' format specifier: -23805.0 32194.0
// </Snippet3>

let callToStringWithSpecifiersAndProviders () =
// <Snippet4>
let value = 14603
let formats =
[ "C"; "D6"; "e1"; "E2"; "F1"; "G"; "N1"
"P0"; "X4"; "000000.0000"; "##000.0" ]
let providers =
[ CultureInfo "en-us"
CultureInfo "fr-fr"
CultureInfo "de-de"
CultureInfo "es-es" ]
// Display header.
printfn $"{providers[0],24}{providers[1],14}{providers[2],14}{providers[3],14}\n"

// Display a value using each format string.
for format in formats do
// Display the value for each provider on the same line.
printf $"{format,-12}"
for provider in providers do
printf $"{value.ToString(format, provider),12} "
printfn ""

// The example displays the following output to the console:
// en-US fr-FR de-DE es-ES
//
// C $14,603.00 14 603,00 € 14.603,00 € 14.603,00 €
// D6 014603 014603 014603 014603
// e1 1.5e+004 1,5e+004 1,5e+004 1,5e+004
// E2 1.46E+004 1,46E+004 1,46E+004 1,46E+004
// F1 14603.0 14603,0 14603,0 14603,0
// G 14603 14603 14603 14603
// N1 14,603.0 14 603,0 14.603,0 14.603,0
// P0 1,460,300 % 1 460 300 % 1.460.300% 1.460.300 %
// X4 390B 390B 390B 390B
// 000000.0000 014603.0000 014603,0000 014603,0000 014603,0000
// ##000.0 14603.0 14603,0 14603,0 14603,0
// </Snippet4>

callDefaultToString ()
printfn "------"
callToStringWithProvider ()
printfn "------"
callToStringWithSpecifiers ()
printfn "------"
callToStringWithSpecifiersAndProviders ()
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="ToString1.fs" />
</ItemGroup>
</Project>
Loading