I need help refining my string formatting

I am trying to format strings based on type ... but I feel this is disgusting. Does anyone have any suggestions for a more sophisticated statement?

 string DateFormat = "MMM dd yyyy";
 string NumberFormat = "0.0";

string FormatString(Type t, object value)
    {
        string output = "";

        switch (t.Name.ToUpper())
        {

            case "DATETIME":
                output = ((DateTime)value).ToString(DateFormat);
                break;
            case "SINGLE":
                output = ((Single)value).ToString(NumberFormat);
                break;
            case "DOUBLE":
                output = ((Double)value).ToString(NumberFormat);
                break;
            default:
                output = value.ToString();
                break;
        }

        return output;

    }
+3
source share
3 answers

Why not just overload the methods?

using System;

namespace test {

    static class Formatter {

        const string DateFormat = "MMM dd yyyy";
        const string NumberFormat = "0.0";

        public static string Format(double d) {
            return d.ToString(NumberFormat);
        }

        public static string Format(DateTime d) {
            return d.ToString(DateFormat);
        }

        // most generic method
        public static string Format(object o) {
            return o.ToString();
        }

    }

    class Program {

        public static void Main() {
            Console.WriteLine(Formatter.Format(2.0d));
            Console.WriteLine(Formatter.Format(DateTime.Now));
            // an integer => no specific function defined => pick the
            // most generic overload (object)
            Console.WriteLine(Formatter.Format(4));
        }

    }
}

NOTE : if you need to compare types, you should use

if (typeof(int) == t){
    // ...
}

and not make comparisons of the type name (or at least if you do), check the names completely , i.e. including namespace).

EDIT
An alternative solution that takes Allon's comment into account using a dictionary type function:

using System;
using System.Collections.Generic;

namespace test {

    public class Formatter {

        delegate string FormatFunction(object o);

        private string FormatDouble(object o) {
            double d = (double)o;
            return d.ToString("0.0");
        }

        private string FormatDateTime(object o) {
            DateTime d = (DateTime)o;
            return d.ToString("MMM dd yyyy");
        }

        // map types to format function
        private Dictionary<Type, FormatFunction> _formatters = new Dictionary<Type, FormatFunction>();

        public Formatter() {
            _formatters.Add(typeof(double), FormatDouble);
            _formatters.Add(typeof(DateTime), FormatDateTime);
        }

        public string Format(object o) { 
            Type t = o.GetType();
            if (_formatters.ContainsKey(t)){
                return _formatters[t](o);
            } else {
                return o.ToString();
            }
        }

    }

    class Program {
        public static void Main() {
            Formatter f = new Formatter();
            Console.WriteLine(f.Format(2.0d));
            Console.WriteLine(f.Format(DateTime.Now));
            Console.WriteLine(f.Format(4));
        }
    }
}

, ( - , ).

+7
string Format<T>(T value)
{
    Type[] numericTypes = new Type[] {typeof(double), typeof(single)};

    if (Array.IndexOf(numericTypes, typeof(T)))
        return string.Format("{0:0.0}", value);
    else if (typeof(T) is DateTime)
        return string.Format("{0:MMM dd yyyy}", value);
    else
        return value.ToString();
}

.Net 3.0 , .

+2

, , , . , . , - . 'is':

public static string FormatObject(object obj)
{
    if (obj is DateTime)
        return ((DateTime)obj).ToString("MMM dd yyyy");
    else if (obj is float || obj is double)
        return (obj as IFormattable).ToString("0.0", new System.Globalization.NumberFormatInfo());
    else return obj.ToString();
}
0

Source: https://habr.com/ru/post/1715715/


All Articles