when you input a money number , such as:12345678.12, maybe you want to change it as"12,345,678.12",coz this format is earier to read.
Now, here is a method we can do it. I know you can use some current method in .NET framwork, but I think it's helpful for you to write some code by yourself.
OK, let's go!
using System.Collections;//add it for ArrayList
namespace FormatMoney
{
class CStack
{
private int p_index;
private ArrayList list;
public CStack()
{
list = new ArrayList();
p_index = -1;
}
public int count
{
get
{
return list.Count;
}
}
public void push(Object item)
{
list.Add(item);
p_index++;
}
public object pop()
{
object obj = list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
}
class Program
{
static void Main(string[] args)
{
CStack alist = new CStack();
string ch;
Console.WriteLine("please input the money number...");
string myMoney = Console.ReadLine().ToString();
for (int i = 0; i < myMoney.Length; i++)
{
alist.push(myMoney.Substring(i,1));
}
int num = 0;
StringBuilder sb = new StringBuilder();
while (alist.count > 0)
{
ch = alist.pop().ToString();
if (ch != ".")
{
if (num == 3)
{
sb.Append("," + ch);
num = 1;
}
else
{
sb.Append(ch);
num++;
}
}
else
{
sb.Append(ch);
num = 0;
}
}
StringBuilder sbOut = new StringBuilder();
for (int i = sb.Length-1; i > -1; i--)
{
sbOut.Append(sb.ToString().Substring(i,1));
}
Console.WriteLine(sbOut.ToString());
}
}
}
Friday, September 26, 2008
Subscribe to:
Posts (Atom)

