将数值型数据格式化成MONEY的几种方法。

1.在SQL SERVER中:
select PCCode,Project,
   substring(convert(varchar, cast(ActualHWK as money),1),1,
 datalength(convert(varchar, cast(ActualHWK as money),1))-3) ActualHWK
,  substring(convert(varchar, cast(BudgetHWK as money),1),1,
 datalength(convert(varchar, cast(BudgetHWK as money),1))-3) BudgetHWK
,  substring(convert(varchar, cast(ForecastHWK as money),1),1,
 datalength(convert(varchar, cast(ForecastHWK as money),1))-3) ForecastHWK
from FactITFin
where SourceSystemID = 3

这个主要是使用了convert(varchar, cast(ActualHWK as money)这个函数,比如12345如果被这样格式化后会变成12,345.00


2. 使用C#中ToString("{0:C}"),如果要使用绑定字段的话,注意将HtmlEncode属性设置成false

3.自己在程序中控制。

 public static string FormatCurrency1(string cMoney)
        {
            if (cMoney == null || cMoney == string.Empty)
            {
                return "0";
            }
            int money = int.Parse(cMoney);
            if (money <= 0)
                return "0";
            string tmp = money.ToString();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            while (tmp.Length > 3)
            {
                string ttt = tmp.Substring(tmp.Length - 3);
                tmp = tmp.Substring(0, tmp.Length - 3);
                sb.Insert(0,ttt);
                if (tmp.Length > 0)
                    sb.Insert(0,",");
            }
            sb.Insert(0,tmp);
            return sb.ToString();
        }

你可能感兴趣的:(将数值型数据格式化成MONEY的几种方法。)