Convert decimal to string I wrote a C# program that converts decimal to string. It is not the best but it works. I'm looking for inputs from others on how can it be improved? One aspect that baffled me was how to find out total number of digits after decimal point? Right now I've to pass it as a separate parameter. It is in C#, but I'm open to any programming language. credit goes to all authors that I found online and little bit to me

    static void Main(string[] args)
    {           
        string s = decToStr(123456.789M,3);
        Console.WriteLine(s);
        Console.ReadLine();            
    }

    static string decToStr(decimal d,double totaldec)
    {            
        if (d == 0)
            return "0";
        int i = (int)d;
        decimal fracPart = d - i;
        int fracPartInt = 0;

        StringBuilder strResult = new StringBuilder();
        StringBuilder fracStrResult = new StringBuilder();         

        strResult = ProcessInt(strResult,i);
        //not sure how to find total number of digts after decimal point 
        fracPartInt = (int)(fracPart * (decimal)Math.Pow(10, totaldec));

        fracStrResult = ProcessInt(fracStrResult, fracPartInt);

        strResult.Append(".").Append(fracStrResult);         

        return strResult.ToString();
    }

    static StringBuilder ProcessInt(StringBuilder sb, int i)
    {
        string digits = "0123456789";
        while (i &gt; 0)
        {                
            sb.Insert(0, digits[i % 10]);
            i = i / 10;
        }

        return sb;

    }
You don't really need to know the length of the fractional part.  Simply separate the fractional from the integral and handle each one separately.

static string DecToStr(decimal value)
{
    int integral = 0;
    decimal fractional = 0;
    SplitDecimal(value, out integral, out fractional);
    StringBuilder sb = new StringBuilder();
    while(integral &gt; 0)
    {
        sb.Insert(0,integral % 10);
        integral /= 10;
    }
    sb.Append('.');
    while(fractional != 0)
    {
        int temp = 0;
        SplitDecimal(fractional * 10, out temp, out fractional);
        sb.Append(temp);
    }
    return sb.ToString();
}
static void SplitDecimal(decimal value,out int integral,out decimal fractional)
{
    integral = (int)value;
    fractional = value - integral;
}


EDIT  added check for negative

static string DecToStr(decimal value)
{
    int integral = 0;
    decimal fractional = 0;
    StringBuilder sb = new StringBuilder();
    if(value &lt; 0)
    {
        value *= -1;
        sb.Append('-');
    }
    SplitDecimal(value, out integral, out fractional);
    while(integral &gt; 0)
    {
        sb.Insert(0,integral % 10);
        integral /= 10;
    }
    sb.Append('.');
    while(fractional != 0)
    {
        int temp = 0;
        SplitDecimal(fractional * 10, out temp, out fractional);
        sb.Append(temp);
    }
    return sb.ToString();
}
static void SplitDecimal(decimal value,out int integral,out decimal fractional)
{
    integral = (int)value;
    fractional = value - integral;
}