'Align number with decimal position in print c#

I have a document to print. I have written this document with filestream and streamwriter. I want the numbers to be aligned with decimal point.

Eg:

      Debit
--------------
200000000.00
     1000.00
       45.00

How to achieve this. The line I have used is

(Convert.ToInt32(dtNewLedger.Rows[intCount]["Dr"]) > 0 ?  **(bkup.PadString(string.Format("{0,0:n2}", dtNewLedger.Rows[intCount]["Dr"]).ToString().Trim(),40-strByTo.Length, "R"))** : new string(' ', 10) )+ " " +(Convert.ToInt32(dtNewLedger.Rows[intCount]["Cr"]) > 0 ? **(bkup.PadString(string.Format("{0,0:n2}", dtNewLedger.Rows[intCount]["Cr"]).ToString().Trim(), 60-strByTo.Length, "R"))** : new string(' ', 10));

Method PadString() is:-

public string PadString(string strWorkStr, int intWidth, string strAlign = "", object varCharCode = null)
{
   string strTemp;
   int intWorkStr;
   int intN;
   varCharCode = "";
   strTemp = new string(' ', intWidth);
   intWorkStr = strWorkStr.Length;
   if (varCharCode == "") varCharCode = " ";
   if (strAlign == "") strAlign = "L";

   if (strAlign == "L")
   {
      if ((intWidth > intWorkStr))
         strTemp = strWorkStr + new string(Convert.ToChar(varCharCode), Convert.ToInt32(intWidth - intWorkStr));
      else
         strTemp = strWorkStr.Substring(intWidth);
   }
   else if (strAlign == "R")
   {
      if (intWidth > intWorkStr)
         strTemp = new string(Convert.ToChar(varCharCode), Convert.ToInt32(intWidth - intWorkStr)) + strWorkStr;
      else
         strTemp = strWorkStr.Substring(intWidth);
   }
   else
   {
      if (intWorkStr < intWidth)
      {
         intN = (intWidth - intWorkStr) / 2;
         if ((intWidth - intWorkStr) % 2 == 0)
            strTemp = new string(Convert.ToChar(varCharCode), Convert.ToInt32(intN)) + strWorkStr + new string(Convert.ToChar(varCharCode), Convert.ToInt32(intN));
         else
            strTemp = new string(Convert.ToChar(varCharCode), Convert.ToInt32(intN + 1)) + strWorkStr + new string(Convert.ToChar(varCharCode), Convert.ToInt32(intN));
      }
      else
         strTemp = strWorkStr.Substring(intWidth);
   }
   return strTemp;
}

Please help. Please let me know if additional details are required.



Solution 1:[1]

Try use string.Format(...)

Please, read this article.

My code example; e.Graphics.DrawString(String.Format("{0,8:##.00}", debit), fntHeadBold, sb, 700, 205 )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tahir FEYZIOGLU