Skip to content

Commit 3956181

Browse files
authored
System.FormatException F# snippets (#7727)
* FormatException F# snippets * fix project file
1 parent 8e005e9 commit 3956181

File tree

12 files changed

+230
-1
lines changed

12 files changed

+230
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module example1
2+
3+
// <Snippet1>
4+
open System
5+
6+
type TemperatureScale =
7+
| Celsius = 0
8+
| Fahrenheit = 1
9+
| Kelvin = 2
10+
11+
let getCurrentTemperature () =
12+
let dat = DateTime.Now
13+
let temp = 20.6m
14+
let scale = TemperatureScale.Celsius
15+
String.Format("At {0:t} on {1:D}, the temperature is {2:F1} {3:G}", dat, temp, scale)
16+
17+
getCurrentTemperature ()
18+
|> printfn "%s"
19+
20+
// The example displays output like the following:
21+
// Unhandled Exception: System.FormatException: Format specifier was invalid.
22+
// at System.Number.NumberToString(ValueStringBuilder& sb, NumberBuffer& number, Char format, Int32 nMaxDigits, NumberFormatInfo info)
23+
// at System.Number.TryFormatDecimal(Decimal value, ReadOnlySpan`1 format, NumberFormatInfo info, Span`1 destination, Int32& charsWritten)
24+
// at System.Decimal.TryFormat(Span`1 destination, Int32& charsWritten, ReadOnlySpan`1 format, IFormatProvider provider)
25+
// at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
26+
// at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
27+
// at System.String.Format(String format, Object arg0, Object arg1, Object arg2)
28+
// at Example.getCurrentTemperature()
29+
// at <StartupCode$fs>.$Example.main@()
30+
// </Snippet1>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module example2
2+
3+
// <Snippet2>
4+
open System
5+
6+
type TemperatureScale =
7+
| Celsius = 0
8+
| Fahrenheit = 1
9+
| Kelvin = 2
10+
11+
let getCurrentTemperature () =
12+
let dat = DateTime.Now
13+
let temp = 20.6m
14+
let scale = TemperatureScale.Celsius
15+
String.Format("At {0:t} on {0:D}, the temperature is {1:F1} {2:G}", dat, temp, scale)
16+
17+
getCurrentTemperature ()
18+
|> printfn "%s"
19+
20+
// The example displays output like the following:
21+
// At 10:40 AM on Wednesday, June 04, 2014, the temperature is 20.6 Celsius
22+
// </Snippet2>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module example3
2+
3+
open System
4+
5+
let n1 = 10
6+
let n2 = 20
7+
// <Snippet4>
8+
let result = String.Format("{0} + {1} = {2}", n1, n2, n1 + n2)
9+
// </Snippet4>
10+
printfn $"{result}"
11+
12+
do
13+
// <Snippet3>
14+
let n1 = 10
15+
let n2 = 20
16+
String result = String.Format("{0 + {1] = {2}",
17+
n1, n2, n1 + n2)
18+
// </Snippet3>
19+
|> ignore
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="example1.fs" />
9+
<Compile Include="example2.fs" />
10+
<Compile Include="example3.fs" />
11+
<Compile Include="iformattable1.fs" />
12+
<Compile Include="iformattable2.fs" />
13+
<Compile Include="iformattable3.fs" />
14+
<Compile Include="iformattable4.fs" />
15+
<Compile Include="qa1.fs" />
16+
<Compile Include="qa2.fs" />
17+
<Compile Include="qa3.fs" />
18+
</ItemGroup>
19+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module ifromattable1
2+
3+
// <Snippet7>
4+
let price = 169.32m
5+
printfn $"The cost is {price:Q2}."
6+
// The example displays the following output:
7+
// Unhandled Exception: System.FormatException: Format specifier was invalid.
8+
// at System.Number.NumberToString(ValueStringBuilder& sb, NumberBuffer& number, Char format, Int32 nMaxDigits, NumberFormatInfo info)
9+
// at System.Number.TryFormatDecimal(Decimal value, ReadOnlySpan`1 format, NumberFormatInfo info, Span`1 destination, Int32& charsWritten)
10+
// at System.Decimal.TryFormat(Span`1 destination, Int32& charsWritten, ReadOnlySpan`1 format, IFormatProvider provider)
11+
// at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
12+
// at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
13+
// at [email protected](Object vobj)
14+
// at Microsoft.FSharp.Core.PrintfImpl.PrintfEnv`3.RunSteps(Object[] args, Type[] argTys, Step[] steps)
15+
// at Microsoft.FSharp.Core.PrintfModule.gprintf[a,TState,TResidue,TResult,TPrinter](FSharpFunc`2 envf, PrintfFormat`4 format)
16+
// at <StartupCode$fs>.$Example.main@()
17+
// </Snippet7>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module iformattable2
2+
3+
// <Snippet8>
4+
let price = 169.32m
5+
printfn $"The cost is {price:C2}."
6+
// The example displays the following output:
7+
// The cost is $169.32.
8+
// </Snippet8>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module iformattable3
2+
3+
// <Snippet9>
4+
open System
5+
6+
let guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"
7+
printfn $"""{Guid.ParseExact(guidString, "G")}"""
8+
// The example displays the following output:
9+
// Unhandled Exception: System.FormatException:
10+
// Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
11+
// at System.Guid.ParseExact(String input, String format)
12+
// at <StartupCode$fs>.$Example.main@()
13+
// </Snippet9>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module iformattable4
2+
3+
// <Snippet10>
4+
open System
5+
6+
let guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"
7+
printfn $"{Guid.Parse guidString}"
8+
// The example displays the following output:
9+
// ba748d5c-ae5f-4cca-84e5-1ac5291c38cb
10+
// </Snippet10>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module qa1
2+
3+
// <Snippet5>
4+
open System
5+
6+
let rnd = Random()
7+
let numbers = Array.zeroCreate<int> 4
8+
let mutable total = 0
9+
for i = 0 to 2 do
10+
let number = rnd.Next 1001
11+
numbers[i] <- number
12+
total <- total + number
13+
numbers[3] <- total
14+
Console.WriteLine("{0} + {1} + {2} = {3}", numbers)
15+
16+
// The example displays the following output:
17+
// Unhandled Exception:
18+
// System.FormatException:
19+
// Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
20+
// at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
21+
// at System.IO.TextWriter.WriteLine(String format, Object arg0)
22+
// at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
23+
// at <StartupCode$fs>.$Example.main@()
24+
// </Snippet5>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module qa2
2+
3+
// <Snippet6>
4+
open System
5+
6+
let rnd = Random()
7+
let numbers = Array.zeroCreate<int> 4
8+
let mutable total = 0
9+
for i = 0 to 2 do
10+
let number = rnd.Next 1001
11+
numbers[i] <- number
12+
total <- total + number
13+
numbers[3] <- total
14+
let values = Array.zeroCreate<obj> numbers.Length
15+
numbers.CopyTo(values, 0)
16+
Console.WriteLine("{0} + {1} + {2} = {3}", values)
17+
// The example displays output like the following:
18+
// 477 + 956 + 901 = 2334
19+
// </Snippet6>

0 commit comments

Comments
 (0)