Home

Introduction

 

Fundamentals

 

Introduction

 

 

     
 
 

 

   
   
 

Previous Copyright © 2015 FunctionX Next

Home

Built-In Interfaces: IFormatProvider

   

Formatting a Value

The collection of techniques and formulas used by a language to display its values is referred to as a format provider. When you use a variable that uses a particular formula to display its value, to help you specify the right formula, the .NET Framework provides the IFormatProvider interface. IFormatProvider is defined in the System namespace.

As mentioned for disposing of an object, there are two main ways you can use the IFormatProvider. You can create a class that implements it. The IFormatProvider is equipped with only one method: GetFormat.

In most cases, you will use classes that already implement the IFormatProvider interface. Those classes are equipped with an overridden version of the ToString() method that takes IFormatProvider as argument. Its signature is:

abstract ToString : 
        provider:IFormatProvider -> string  
override ToString : 
        provider:IFormatProvider -> string

This method requires that you build an IFormatProvider object and pass it as argument. An alternative is to pass a string. This is possible with another version of the ToString() method whose signature is:

member ToString : 
        format:string -> string

 This method takes a string as argument. The string can take a character as one of the following:

Character Description
c C Currency values
d D Decimal numbers
e E Scientific numeric display such as 1.45e5
f F Fixed decimal numbers
d D General and most common type of numbers
n N Natural numbers
r R Roundtrip formatting
s S Hexadecimal formatting
p P Percentages

Here are examples:

open System
open System.Windows.Forms

type IPolygon =
    interface
        abstract Side            : float with get, set
        abstract SidesCount      : int   with get
        abstract member Angle    : float with get
        abstract Perimeter       : float with get
        abstract member Area     : float with get
        abstract member Describe : unit -> string
    end

type Octagon(side : float) =
    let sd = ref side

    interface IPolygon with
        member this.Side       with get() = !sd and set(value) = sd := value
        member this.SidesCount with get() = 8
        member this.Angle      with get() = 135.00
        member this.Perimeter  with get() = !sd  * float (this :> IPolygon).SidesCount
        member this.Area       with get() = 2.00 * (1.00 + (sqrt 2.00)) * !sd * !sd
        member this.Describe() = "An octagon is a polygon with 8 equal sides. The angle between 2 adjacent sides is 135 degrees."

// Form: Polygons: Hexagon
let geometry = new Form(Width = 316, Height = 250, Text = "Polygon: Octagon")

// Side Value
geometry.Controls.Add(new Label(Left = 25, Top = 26, Width = 64, Text = "Side Value:"))
let txtSideValue = new TextBox(Left = 102, Top = 23, Text = "0.00")
geometry.Controls.Add txtSideValue

// Button: Calculate
let btnCalculate = new Button(Left = 208, Top = 21, Text = "Calculate")
geometry.Controls.Add btnCalculate

// Line
geometry.Controls.Add(new Label(Left = 22, Top = 49, Width = 275, Text = "-----------------------------------------------------------------------"))

// Side Count
geometry.Controls.Add(new Label(Left = 25, Top = 75, Width = 64, Text = "Side Count:"))
let txtSideCount = new TextBox(Left = 102, Top = 72, Text = "0")
geometry.Controls.Add txtSideCount

// Angle
geometry.Controls.Add(new Label(Left = 25, Top = 101, Width = 64, Text = "Angle:"))
let txtAngle = new TextBox(Left = 102, Top = 98, Text = "0.00")
geometry.Controls.Add txtAngle
geometry.Controls.Add(new Label(Left = 157, Top = 101, Width = 64, Text = "Degrees"))

// Perimeter
geometry.Controls.Add(new Label(Left = 25, Top = 122, Width = 64, Text = "Perimeter:"))
let txtPerimeter = new TextBox(Left = 102, Top = 124, Text = "0.00")
geometry.Controls.Add txtPerimeter

// Area
geometry.Controls.Add(new Label(Left = 25, Top = 150, Width = 40, Text = "Area:"))
let txtArea = new TextBox(Left = 102, Top = 150, Text = "0.00")
geometry.Controls.Add txtArea

// Description
let lblDescription = new Label(Left = 25, Top = 182, Width = 258, Height = 32, Text = "Description")
geometry.Controls.Add lblDescription

let btnCalculateClick e =
    let side = float txtSideValue.Text

    let oct = Octagon(side)
    let sides = (oct :> IPolygon).SidesCount
    let angle = (oct :> IPolygon).Angle
    let perimeter = (oct :> IPolygon).Perimeter
    let area = (oct :> IPolygon).Area

    txtSideCount.Text <- sides.ToString()
    txtAngle.Text <- angle.ToString("N")
    txtPerimeter.Text <- perimeter.ToString("F")
    txtArea.Text <- area.ToString("e")
    lblDescription.Text <- (oct :> IPolygon).Describe()

btnCalculate.Click.Add btnCalculateClick

// Button: Close
let btnClose = new Button(Left = 208, Top = 148, Text = "Close")
let btnCloseClick e = geometry.Close()
btnClose.Click.Add btnCloseClick
geometry.Controls.Add btnClose

do Application.Run geometry

Here is an example of executing the program:

Formatting a String


Home Copyright © 2014-2015 FunctionX Home