ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

ToString

Returns a formatted string on a nullable double

Source

/// <summary>
/// Returns a formatted double or emtpy string
/// </summary>
/// <param name="t">double or null</param>
/// <param name="format">double formatstring </param>
/// <returns></returns>
public static string ToString(this double? t, string format) 
{
    if (t != null)
    {
        return t.Value.ToString(format);
    }

    return "";
}

Example

double? pi = null;

string s = pi.ToString("0.00"); //s = ""
pi = Math.Pi;

s = pi.ToString("0.00");  //s = 3.14

Author: C.F. Meijers

Submitted on: 11 dec 2007

Language: C#

Type: System.Double

Views: 3639