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

Skip to content

System.IFormattable F# snippets #7769

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
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
92 changes: 92 additions & 0 deletions snippets/fsharp/System/IFormattable/Overview/example1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// <Snippet1>
open System
open System.Globalization

type Temperature(temperature: decimal) =
do
if temperature < -273.15M then
raise (ArgumentOutOfRangeException $"{temperature} is less than absolute zero.")

member _.Celsius =
temperature

member _.Fahrenheit =
temperature * 9M / 5M + 32M

member _.Kelvin =
temperature + 273.15m

override this.ToString() =
this.ToString("G", CultureInfo.CurrentCulture)

member this.ToString(format) =
this.ToString(format, CultureInfo.CurrentCulture)

member this.ToString(format, provider: IFormatProvider) =
let format =
if String.IsNullOrEmpty format then "G"
else format

let provider =
if isNull provider then
CultureInfo.CurrentCulture :> IFormatProvider
else provider

match format.ToUpperInvariant() with
| "G" | "C" ->
temperature.ToString("F2", provider) + " °C"
| "F" ->
this.Fahrenheit.ToString("F2", provider) + " °F"
| "K" ->
this.Kelvin.ToString("F2", provider) + " K"
| _ ->
raise (FormatException $"The {format} format string is not supported.")

interface IFormattable with
member this.ToString(format, provider) = this.ToString(format, provider)

// </Snippet1>

// <Snippet2>
open System
open System.Globalization

[<EntryPoint>]
let main _ =
// Use composite formatting with format string in the format item.
let temp1 = Temperature 0
Console.WriteLine("{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)

// Use composite formatting with a format provider.
let temp1 = Temperature -40
String.Format(CultureInfo.CurrentCulture, "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)", temp1)
|> printfn "%s"
String.Format(CultureInfo "fr-FR", "{0:C} (Celsius) = {0:K} (Kelvin) = {0:F} (Fahrenheit)\n", temp1)
|> printfn "%s"

// Call ToString method with format string.
let temp1 = Temperature 32
Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)\n",
temp1.ToString "C", temp1.ToString "K", temp1.ToString "F")

// Call ToString with format string and format provider
let temp1 = Temperature 100
let current = NumberFormatInfo.CurrentInfo
let nl = CultureInfo "nl-NL"
Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
temp1.ToString("C", current), temp1.ToString("K", current), temp1.ToString("F", current))
Console.WriteLine("{0} (Celsius) = {1} (Kelvin) = {2} (Fahrenheit)",
temp1.ToString("C", nl), temp1.ToString("K", nl), temp1.ToString("F", nl))
0

// The example displays the following output:
// 0.00 °C (Celsius) = 273.15 K (Kelvin) = 32.00 °F (Fahrenheit)
//
// -40.00 °C (Celsius) = 233.15 K (Kelvin) = -40.00 °F (Fahrenheit)
// -40,00 °C (Celsius) = 233,15 K (Kelvin) = -40,00 °F (Fahrenheit)
//
// 32.00 °C (Celsius) = 305.15 K (Kelvin) = 89.60 °F (Fahrenheit)
//
// 100.00 °C (Celsius) = 373.15 K (Kelvin) = 212.00 °F (Fahrenheit)
// 100,00 °C (Celsius) = 373,15 K (Kelvin) = 212,00 °F (Fahrenheit)
// </Snippet2>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IFormattable/Overview/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="example1.fs" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions xml/System/IFormattable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@
The following example defines a `Temperature` class that implements the <xref:System.IFormattable> interface. The class supports four format specifiers: "G" and "C", which indicate that the temperature is to be displayed in Celsius; "F", which indicates that the temperature is to be displayed in Fahrenheit; and "K", which indicates that the temperature is to be displayed in Kelvin. In addition, the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation also can handle a format string that is `null` or empty. The other two `ToString` methods defined by the `Temperature` class simply wrap a call to the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation.

:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1":::

The following example then calls the <xref:System.IFormattable.ToString%2A?displayProperty=nameWithType> implementation either directly or by using a composite format string.

:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet2":::

]]></format>
Expand Down Expand Up @@ -189,6 +191,7 @@
The following example demonstrates a `Temperature` class that implements the <xref:System.IFormattable.ToString%2A> method. This code example is part of a larger example provided for the <xref:System.IFormattable> class.

:::code language="csharp" source="~/snippets/csharp/System/IFormattable/Overview/example1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/IFormattable/Overview/example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.iformattable/vb/example1.vb" id="Snippet1":::

]]></format>
Expand Down