int nIndex; nIndex=strResult.Find('.'); if (nIndex>=0) { strResult.TrimRight('0'); if (strResult.GetLength()==nIndex+1) { strResult=strResult.Left(nIndex); if (strResult.IsEmpty()) strResult='0'; } }
正确答案是:string.Format("{0:0.#####}",0.0030)
实在受不了了,什么答案啊都 decimal dec = 2.50000 去掉小数点 dec.ToString("#") 保留2位小数点dec.ToString("#.##") 或dec.ToString("N")
string s = dec.ToString("0.0");
string temp = dec.ToString();
while(true)
{
if(temp[temp.length-1]=='0')
temp=temp.RemoveAt(temp.length);
else
break;
}int float=float.Parse(temp); //num=2.5
string temp = dec.ToString();
while(true)
{
if(temp[temp.length-1]=='0')
temp=temp.RemoveAt(temp.length);
else
break;
}float num=float.Parse(temp); //num=2.5
string s = dec.ToString().TrimEnd('0');
string s = dec.ToString().TrimEnd('0');
if (s.EndsWith(".")) s = s.Substring(0, s.Length - 1);
viena() 维也纳的答案,输入250,得到25,也错误这样才对吧
decimal dec = 2.50000m;
string s = dec.ToString("0.0");
if(s.EndsWith("0"))
s = s.Substring(0,s.Length -2);
在这里更正一下:
temp=temp.Remove(ss.Length-1, 1)真是不好意思
第一个答案的意思是固定输出格式为小数点与小数点前后必须各有一位
2.50000碰巧符合这个特征,如果是其他数就可能不对了
=========================================================================
貌似不对吧?
decimal dec = 2555.50000m;
Console.WriteLine( dec.ToString("0.0") );
这个输出的是2555.5,也就是说小数点前不固定,小数点后有一位,是吧?
float flt = float.Parse(dec.ToString());
Console.WriteLine(dec);
Console.WriteLine(flt);
Console.Read();
--------显示结果----------------
2.5000
2.5Good Luck!
Tony
string tempR="",tempL="";
try
{
tempL=values.Substring(0,values.ToString().IndexOf("."));
tempR=values.Substring(values.ToString().IndexOf(".")+1,values.ToString().Length-values.ToString().IndexOf(".")-1);
for(int i=tempR.Length-1;i>=1;i++)
{
if(tempR.Substring(i,1).ToString()=="0")
{
tempR=tempR.Substring(0,tempR.Length-1);
i=tempR.Length-2;
}
}
values=tempL.ToString()+"."+tempR.ToString();
}
catch
{
}
this.textBox1.Text=values.ToString();
float flt = float.Parse(dec.ToString());这个应该对的
dc.ToString("G0")
-----------------
这个才是正解...转换成float或double可能会丢失精度...
string temp = dec.ToString();
while(true)
{
if(temp[temp.length-1]=='0')
temp=temp.RemoveAt(temp.length);
else
break;
}int float=float.Parse(temp); //num=2.5
strResult.Format("%f",fData);
int nIndex;
nIndex=strResult.Find('.');
if (nIndex>=0)
{
strResult.TrimRight('0');
if (strResult.GetLength()==nIndex+1)
{
strResult=strResult.Left(nIndex);
if (strResult.IsEmpty())
strResult='0';
}
}
decimal dec = 2.50000
去掉小数点 dec.ToString("#")
保留2位小数点dec.ToString("#.##") 或dec.ToString("N")