日期:2014-05-19  浏览次数:20976 次

怎样写这个将double格式化为字符串的格式化字符串。
我想把double型数据格式化为一个字符串,
我现在用的是如:
double   dblA=3.000189;
string   str=dblA.ToString( "F3 ");//等到str=3.000
但我想得到的是3,而不是3.000,即我最多想精确到小数点后三位,但如果后面全是0的话,那些0我都不要。该怎样写这个格式化表达式?

------解决方案--------------------
自己写程序判断。
------解决方案--------------------
double dblA = 3.000189;
int Temp = Convert.ToInt32(dblA);
string str = Temp.ToString;
------解决方案--------------------
public static decimal FMT_Decimal(string strnum,int x)
{
decimal y;
string tmp;
try
{
y=Convert.ToDecimal(strnum);
if (strnum.IndexOf( ". ") <0) return y;
y=Math.Round(y,x);
tmp=y.ToString();
tmp=tmp.Replace( "0 ", " ").Trim();
if (tmp.EndsWith( ". ")) tmp=tmp.Substring(0,tmp.Length-1);
tmp=tmp.Replace( " ", "0 ");
y=Convert.ToDecimal(tmp);
return y;
}
catch
{
return 0;
}
}