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

Skip to content

System.SByte F# snippets #7836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2022
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
17 changes: 17 additions & 0 deletions snippets/fsharp/System/SByte/MaxValue/MaxValue1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
open System

// <Snippet1>
let longValue = -130L

if longValue <= int64 SByte.MaxValue && longValue >= int64 SByte.MinValue then
let byteValue = int8 longValue
printfn $"Converted long integer value to {byteValue}."
else
let rangeLimit, relationship =
if longValue > int64 SByte.MaxValue then
SByte.MaxValue, "greater"
else
SByte.MinValue, "less"

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

<ItemGroup>
<Compile Include="parseex1.fs" />
<Compile Include="parseex2.fs" />
<Compile Include="parseex3.fs" />
<Compile Include="parse_1.fs" />
</ItemGroup>
</Project>
74 changes: 74 additions & 0 deletions snippets/fsharp/System/SByte/Parse/parse_1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module parse_1

// <Snippet2>
open System
open System.Globalization

let provider = NumberFormatInfo.CurrentInfo

let callParseOperation stringValue (style: NumberStyles) =
if stringValue = null then
printfn "Cannot parse a null string..."
else
try
let number = SByte.Parse(stringValue, style)
printfn $"SByte.Parse('{stringValue}', {style})) = {number}"
with
| :? FormatException ->
printfn $"'{stringValue}' and {style} throw a FormatException"
| :? OverflowException ->
printfn $"'{stringValue}' is outside the range of a signed byte"

[<EntryPoint>]
let main _ =
let stringValue = " 123 "
let style = NumberStyles.None
callParseOperation stringValue style

let stringValue = "000,000,123"
let style = NumberStyles.Integer ||| NumberStyles.AllowThousands
callParseOperation stringValue style

let stringValue = "-100"
let style = NumberStyles.AllowLeadingSign
callParseOperation stringValue style

let stringValue = "100-"
let style = NumberStyles.AllowLeadingSign
callParseOperation stringValue style

let stringValue = "100-"
let style = NumberStyles.AllowTrailingSign
callParseOperation stringValue style

let stringValue = "$100"
let style = NumberStyles.AllowCurrencySymbol
callParseOperation stringValue style

let style = NumberStyles.Integer
callParseOperation stringValue style

let style = NumberStyles.AllowDecimalPoint
callParseOperation "100.0" style

let stringValue = "1e02"
let style = NumberStyles.AllowExponent
callParseOperation stringValue style

let stringValue = "(100)"
let style = NumberStyles.AllowParentheses
callParseOperation stringValue style
0

// The example displays the following information to the console:
// ' 123 ' and None throw a FormatException
// SByte.Parse('000,000,123', Integer, AllowThousands)) = 123
// SByte.Parse('-100', AllowLeadingSign)) = -100
// '100-' and AllowLeadingSign throw a FormatException
// SByte.Parse('100-', AllowTrailingSign)) = -100
// SByte.Parse('$100', AllowCurrencySymbol)) = 100
// '$100' and Integer throw a FormatException
// SByte.Parse('100.0', AllowDecimalPoint)) = 100
// SByte.Parse('1e02', AllowExponent)) = 100
// SByte.Parse('(100)', AllowParentheses)) = -100
// </Snippet2>
31 changes: 31 additions & 0 deletions snippets/fsharp/System/SByte/Parse/parseex1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module parseex1

// <Snippet1>
open System

// Define an array of numeric strings.
let values =
[| "-16"; " -3"; "+ 12"; " +12 "; " 12 "
"+120"; "(103)"; "192"; "-160" |]

// Parse each string and display the result.
for value in values do
try
printfn $"Converted '{value}' to the SByte value {SByte.Parse value}."
with
| :? FormatException ->
printfn $"'{value}' cannot be parsed successfully by SByte type."
| :? OverflowException ->
printfn $"'{value}' is out of range of the SByte type."

// The example displays the following output:
// Converted '-16' to the SByte value -16.
// Converted ' -3' to the SByte value -3.
// '+ 12' cannot be parsed successfully by SByte type.
// Converted ' +12 ' to the SByte value 12.
// Converted ' 12 ' to the SByte value 12.
// Converted '+120' to the SByte value 120.
// '(103)' cannot be parsed successfully by SByte type.
// '192' is out of range of the SByte type.
// '-160' is out of range of the SByte type.
// </Snippet1>
45 changes: 45 additions & 0 deletions snippets/fsharp/System/SByte/Parse/parseex2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module parseex2

// <Snippet2>
open System
open System.Globalization

// Parse value with no styles allowed.
let values1 = [| " 121 "; "121"; "-121" |]
let style = NumberStyles.None
printfn $"Styles: {style}"
for value in values1 do
try
let number = SByte.Parse(value, style)
printfn $" Converted '{value}' to {number}."
with :? FormatException ->
printfn $" Unable to parse '{value}'."
printfn ""

// Parse value with trailing sign.
let style2 = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
let values2 = [| " 103+"; " 103 +"; "+103"; "(103)"; " +103 " |]
printfn $"Styles: {style2}"
for value in values2 do
try
let number = SByte.Parse(value, style2)
printfn $" Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $" Unable to parse '{value}'."
| :? OverflowException ->
printfn $" '{value}' is out of range of the SByte type."
printfn ""
// The example displays the following output:
// Styles: None
// Unable to parse ' 121 '.
// Converted '121' to 121.
// Unable to parse '-121'.
//
// Styles: Integer, AllowTrailingSign
// Converted ' 103+' to 103.
// Converted ' 103 +' to 103.
// Converted '+103' to 103.
// Unable to parse '(103)'.
// Converted ' +103 ' to 103.
// </Snippet2>
37 changes: 37 additions & 0 deletions snippets/fsharp/System/SByte/Parse/parseex3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module parseex3

// <Snippet3>
open System
open System.Globalization

let nf = NumberFormatInfo()
nf.NegativeSign <- "~"

let values = [| "-103"; "+12"; "~16"; " 1"; "~255" |]
let providers: IFormatProvider[] = [| nf; CultureInfo.InvariantCulture |]

for provider in providers do
printfn $"Conversions using {(box provider).GetType().Name}:"
for value in values do
try
printfn $" Converted '{value}' to {SByte.Parse(value, provider)}."
with
| :? FormatException ->
printfn $" Unable to parse '{value}'."
| :? OverflowException ->
printfn $" '{value}' is out of range of the SByte type."

// The example displays the following output:
// Conversions using NumberFormatInfo:
// Unable to parse '-103'.
// Converted '+12' to 12.
// Converted '~16' to -16.
// Converted ' 1' to 1.
// '~255' is out of range of the SByte type.
// Conversions using CultureInfo:
// Converted '-103' to -103.
// Converted '+12' to 12.
// Unable to parse '~16'.
// Converted ' 1' to 1.
// Unable to parse '~255'.
// </Snippet3>
13 changes: 13 additions & 0 deletions snippets/fsharp/System/SByte/ToString/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="tostring2.fs" />
<Compile Include="tostring3.fs" />
<Compile Include="tostring4.fs" />
<Compile Include="tostring5.fs" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions snippets/fsharp/System/SByte/ToString/tostring2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module tostring2

// <Snippet2>
let value = -123y
// Display value using default ToString method.
printfn $"{value.ToString()}" // Displays -123
// Display value using some standard format specifiers.
printfn $"""{value.ToString "G"}""" // Displays -123
printfn $"""{value.ToString "C"}""" // Displays ($-123.00)
printfn $"""{value.ToString "D"}""" // Displays -123
printfn $"""{value.ToString "F"}""" // Displays -123.00
printfn $"""{value.ToString "N"}""" // Displays -123.00
printfn $"""{value.ToString "X"}""" // Displays 85
// </Snippet2>
33 changes: 33 additions & 0 deletions snippets/fsharp/System/SByte/ToString/tostring3.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module tostring3

// <Snippet3>
open System.Globalization

// Define a custom NumberFormatInfo object with "~" as its negative sign.
let nfi = NumberFormatInfo(NegativeSign = "~")

// Initialize an array of SByte values.
let bytes = [| -122y; 17y; 124y |]

// Display the formatted result using the custom provider.
printfn "Using the custom NumberFormatInfo object:"
for value in bytes do
printfn $"{value.ToString nfi}"

printfn ""

// Display the formatted result using the invariant culture.
printfn "Using the invariant culture:"
for value in bytes do
printfn $"{value.ToString NumberFormatInfo.InvariantInfo}"
// The example displays the following output:
// Using the custom NumberFormatInfo object:
// ~122
// 17
// 124
//
// Using the invariant culture:
// -122
// 17
// 124
// </Snippet3>
53 changes: 53 additions & 0 deletions snippets/fsharp/System/SByte/ToString/tostring4.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module tostring4

// <Snippet4>
let values = [| -124y; 0y; 118y |]
let specifiers =
[| "G"; "C"; "D3"; "E2"; "e3"; "F"
"N"; "P"; "X"; "00.0"; "#.0"
"000(0)**Zero**" |]

for value in values do
for specifier in specifiers do
printfn $"{specifier}: {value.ToString specifier}"
printfn ""
// The example displays the following output:
// G: -124
// C: ($124.00)
// D3: -124
// E2: -1.24E+002
// e3: -1.240e+002
// F: -124.00
// N: -124.00
// P: -12,400.00 %
// X: 84
// 00.0: -124.0
// #.0: -124.0
// 000(0)**Zero**: (124)
//
// G: 0
// C: $0.00
// D3: 000
// E2: 0.00E+000
// e3: 0.000e+000
// F: 0.00
// N: 0.00
// P: 0.00 %
// X: 0
// 00.0: 00.0
// #.0: .0
// 000(0)**Zero**: **Zero**
//
// G: 118
// C: $118.00
// D3: 118
// E2: 1.18E+002
// e3: 1.180e+002
// F: 118.00
// N: 118.00
// P: 11,800.00 %
// X: 76
// 00.0: 118.0
// #.0: 118.0
// 000(0)**Zero**: 118
// </Snippet4>
Loading