c语言,C#里怎么对一个小数4舍五入啊?谢谢

C#里怎么对一个小数4舍五入啊?谢谢 - 故障解答 - 电脑教程网

C#里怎么对一个小数4舍五入啊?谢谢

日期:2007-03-23   荐:
C#里怎么对一个小数4舍五入啊?谢谢C#里怎么对一个小数4舍5入啊?谢谢比如:i=1.23564735...怎么4舍5入保留两位啊?用math.round()不过不是中国传统意义上的4舍5入我写了一个方法#region 四舍五入方法/// <summary>/// 四舍五入方法/// </summary>/// <param name="Value">需要四舍五入的值</param>/// <param name="Digits">需要精确的位数,整数位为正,小数位为负。例:精确到小数第二位就是-2。精确到整数第二位就是2</param>/// <returns>四舍五入后的值</returns>public static double MathRound(double Value,int Digits){double d = Convert.ToDouble(Value);if(Digits > 0)d = d / Math.Pow(10,Digits);string fractionString = ""; //小数部分字符串//取得小数点的位置,将字符串分为整数部分和小数部分,没有小数部分就为空。int dotPosition = d.ToString().IndexOf(".");if(dotPosition != -1)fractionString = d.ToString().Substring(dotPosition 1);if(fractionString.Length <= -Digits 1)d = Math.Pow(10,Digits-2);if(Digits <= 0)return Math.Round(d,-Digits);elsereturn Math.Round(d,0) * Math.Pow(10,Digits);}#endregionint d=828.1456567;string st = d.ToString(".##");学习upDecimal taxRate = new Decimal(10.0); Decimal orderTax = (Decimal)order.Tables[OrderData.ORDER_SUMMARY_TABLE].Rows[0][OrderData.SUB_TOTAL_FIELD]; return Decimal.Round (orderTax / taxRate, 2);用 Math.Round 已经足够了。Math.Round(double 原来的数, int 小数点后的位数);http://community.csdn.net/Expert/topic/3629/3629929.xml?temp=.0537836Math.Roundmark//将i四舍五入取小数点后两位float i =1.23756F;i = float.Parse((String.Format("{0:F2}",i)));//结果为1.24
标签: