Am making a pure .net library with helper functions (GitHub).
However I wanted to have a thousand separator for all number types and here is what I am currently doing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Avo
{
public static class Extension_Number
{
#region ThousandSeparator
public static string ToThousandSeparator(this decimal value, int numberOfDecimalPlaces)
{
if (numberOfDecimalPlaces < 0) numberOfDecimalPlaces = 0;
return string.Format("{0:N" + numberOfDecimalPlaces + "}", value).ToString();
}
public static string ToThousandSeparator(this int value, int numberOfDecimalPlaces)
{
if (numberOfDecimalPlaces < 0) numberOfDecimalPlaces = 0;
return string.Format("{0:N" + numberOfDecimalPlaces + "}", value).ToString();
}
public static string ToThousandSeparator(this double value, int numberOfDecimalPlaces)
{
if (numberOfDecimalPlaces < 0) numberOfDecimalPlaces = 0;
return string.Format("{0:N" + numberOfDecimalPlaces + "}", value).ToString();
}
public static string ToThousandSeparator(this long value, int numberOfDecimalPlaces)
{
if (numberOfDecimalPlaces < 0) numberOfDecimalPlaces = 0;
return string.Format("{0:N" + numberOfDecimalPlaces + "}", value).ToString();
}
#endregion
}
}
As you can see above ,
l have to repeat the same for int , decimal , long
l can achieve the results l want but is it best
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Avo
{
class Program
{
static void Main(string[] args)
{
decimal tes = 294944.8484827M;
int iint = 34;
Console.WriteLine(tes.ToThousandSeparator(3));
Console.WriteLine(iint.ToThousandSeparator(2));
Console.ReadKey();
}
}
}
but is there an eloquent way of doing it , but allowing number types only.