日期:2014-05-18  浏览次数:20783 次

怎样方便让浮点型保留2位小数?请前辈指点!
float sdg04=89.43545;
float sdnscd=4353;
float sdnshl=5656.34

float result = sdg04 / ((sdnscd / 100) * (sdnshl / 100));

怎样方便的让result保留2位小数?

(math.round 不支持浮点型)

------解决方案--------------------
强制转化为double再round就是了。
------解决方案--------------------
楼上正解
------解决方案--------------------
float result =Convert.ToSingle((sdg04 / ((sdnscd / 100) * (sdnshl / 100))).ToString("f2"));
------解决方案--------------------
float sdg04 = 89.43545f;
float sdnscd = 4353f;
float sdnshl = 5656.34f;
float result = sdg04 / ((sdnscd / 100) * (sdnshl / 100));
result = (float)Math.Round((double)result, 2);