How to convert an object to a string with a format string?

Shigure

I have an object which may be boxed from int, float, decimal, DateTime, TimeSpan etc. Now I want to convert it to a string with a format string, such as "D4", "P2", or "yyyyMMdd". But the object class doesn't have a ToString method which receives the format string. So the following code doesn't work.

string Format(object value, string format)
{
    return value.ToString(format); // error
}

Below is my ugly workaround.

string Format(object value, string format)
{
    if (value is int) return ((int)value).ToString(format);
    if (value is float) return ((float)value).ToString(format);
    if (value is double) return ((double)value).ToString(format);
    if (value is decimal) return ((decimal)value).ToString(format);
    if (value is DateTime) return ((DateTime)value).ToString(format);
    if (value is TimeSpan) return ((TimeSpan)value).ToString(format);
    ...
}

Is there a smarter way?

xanatos

There is an interface for that, it is IFormattable:

public static string Format(object value, string format, IFormatProvider formatProvider = null)
{
    if (value == null)
    {
        return string.Empty;
    }

    IFormattable formattable = value as IFormattable;

    if (formattable != null)
    {
        return formattable.ToString(format, formatProvider);
    }

    throw new ArgumentException("value");
}

(this interface is the one used by the various string.Format, Console.Write, ...)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert object to string with custom format

How to convert a correctly formatted Javascript object (in string format) to an object?

How to format the given time string and convert to date/time object

How to convert string in a datetime.datetime format into a datetime object?

How to convert the array object data to string format data using golang?

How to convert list of Object into Specified format String using JS

Swift : how to convert String to string with time format

Convert string to date object without using the format

ObjectMapper convert string value(json format) to object

Convert date string format to a datetime Python Object

How to convert Hex in string format to Hex format

How to convert objects or string to time format when the input string/object is malformed that is %H does not exist for all rows

How to convert String to Set<Object>?

How to convert object notation to a string

How to convert object class into string

How to convert a string to datetime object

How to convert the Object[] to String[] in Java?

how to convert object to string in java

How to convert Python Object into String?

How to convert string into object property?

How to convert the string to object in JavaScript?

Android : How To convert Object to String

How to convert List(Of String) to Object()?

How to convert a String into an Object in JavaScript

How to convert an Object to an array of string

How to convert string to a object in typescript

How to convert object into string in javascript?

How to convert string to array of object?

How to convert a String to Object in Flutter?